NumberTheory Part 2
NumberTheory Part 2
NumberTheory Part 2
Generate Divisors of N
We want to find the all divisors of N. We begin by writing the number as a product of prime
factors:
#include <stdio.h>
#include <math.h>
#include <algorithm>
bool flag[SIZE_N+5];
int primes[SIZE_P+5];
int seive()
{
int i,j,total=0,val;
for(i=2;i<=SIZE_N;i++) flag[i]=1;
val=sqrt(SIZE_N)+1;
for(i=2;i<val;i++)
if(flag[i])
for(j=i;j*i<=SIZE_N;j++)
flag[i*j]=0;
for(i=2;i<=SIZE_N;i++)
if(flag[i])
primes[total++]=i;
return total;
}
Number Theory Part 2 2
SUST Programming Camp 2012
val=sqrt(N)+1;
Total_Prime=0;
for(i=0;primes[i]<val;i++)
{
if(N%primes[i]==0)
{
count=0;
while(N%primes[i]==0)
{
N/=primes[i];
count++;
}
store_primes[Total_Prime]=primes[i];
freq_primes[Total_Prime]=count;
Total_Prime++;
val=sqrt(N)+1; // sqrt again for fast compute.
}
}
if(N>1)
{
store_primes[Total_Prime]=N;
freq_primes[Total_Prime]=1;
Total_Prime++;
}
}
void Generate(int cur,int num)
{
int i,val;
if(cur==Total_Prime)
{
store_divisor[ans++]=num;
}
else
{
val=1;
for(i=0;i<=freq_primes[cur];i++)
{
Generate(cur+1,num*val);
val=val*store_primes[cur];
}
}
}
int main()
Number Theory Part 2 3
SUST Programming Camp 2012
{
int total=seive();
int n,i;
while(scanf("%d",&n)==1)
{
divisor(n);
ans=0;
Generate(0,1);
sort(&store_divisor[0],&store_divisor[ans]);
/*
Input:
20
30
15
Output:
Total No of Divisors: 6
1 2 4 5 10 20
Total No of Divisors: 8
1 2 3 5 6 10 15 30
Total No of Divisors: 4
1 3 5 15
*/
Number Theory Part 2 4
SUST Programming Camp 2012
int D[1000010];
void DivisorGenerate()
{
int i,j,val,N,M,count;
D[1]=1;
for(i=2;i<=1000000;i++)
{
N=M=i;
val=sqrt(N)+1;
for(j=0;primes[j]<val;j++)
{
if(M%primes[j]==0)
{
count=0;
while(M%primes[j]==0)
{
M/=primes[j];
count++;
}
D[N]=(count+1)*D[M];
break;
}
}
if(M==N) //Special Case if N equal prime
{
D[N]=2;
}
}
}
Number Theory Part 2 5
SUST Programming Camp 2012
C++ Code:
#include <stdio.h>
#include <math.h>
#include <algorithm>
bool flag[SIZE_N+5];
int primes[SIZE_P+5];
int seive()
{
int i,j,total=0,val;
for(i=2;i<=SIZE_N;i++) flag[i]=1;
val=sqrt(SIZE_N)+1;
for(i=2;i<val;i++)
if(flag[i])
for(j=i;j*i<=SIZE_N;j++)
flag[i*j]=0;
Number Theory Part 2 7
SUST Programming Camp 2012
for(i=2;i<=SIZE_N;i++)
if(flag[i])
primes[total++]=i;
return total;
}
int factors_in_factorial(int N,int p)
{
int sum=0;
while(N)
{
sum+=N/p;
N/=p;
}
return sum;
}
int Trailingzero_Base_B(int N,int B)
{
int i,ans,freq,power;
ans=1000000000;
for(i=0;primes[i]<=B;i++)
{
if(B%primes[i]==0)
{
freq=0;
while(B%primes[i]==0)
{
freq++;
B/=primes[i];
}
power=factors_in_factorial(N,primes[i]);
ans=min(ans,power/freq);
}
}
return ans;
}
int main()
{
int total=seive();
int i,N,B,zero;
while(scanf("%d %d",&N,&B)==2)
{
zero=Trailingzero_Base_B(N,B);
printf("%d\n",zero);
}
return 0;
}
Number Theory Part 2 8
SUST Programming Camp 2012
We want to last non zero digit of Factorial N. Example: 5! = 120, here last non zero digit 2. How
calculate? We know any factorial can be represented by prime factor.
But we know that, pair(5,2) create a trailing zero and power of 5(q5) is less than power of 2(q2). So we
discard it for this problem
L(N)=(N!”)%10
=(( 2q2-q5 %10) x (3q3 %10) x (7q7 %10) x (11q11 %10) x (13q13%10) …….)%10
L(0)=1
L(1)=1
L(2)=2
L(3)=6
L(4)=4
L(N)= (2N/5 x L(N/5) x L(N%5) )%10
int PTwo(int N)
{
int T[]={6,2,4,8};
if(N==0) return 1;
return T[N%4];
}
int LastNZDigit(int N)
{
int A[]={1,1,2,6,4};
return (PTwo(N/5)*LastNZDigit(N/5)*LastNZDigit(N%5))%10;
}
Number Theory Part 2 9
SUST Programming Camp 2012
GCD(a, b) = GCD(-a, b)
GCD(a,0) = |a|
If a and b are any integers, not both zero, then GCD(a,b) is the smallest positive
element of the set {ax + by : x, y in Z} of linear combinations of a and b.
LCM(a,b)=(a*b)/GCD(a,b)
Given a and b. Extended Euclidean Algorithm to find x, y and gcd(a,b). Form This equation
ax+by=gcd(a,b)
Congruence
Definition: Two integers a and b are said to be congruence (or equivalent) module an
integer m — written a ≡ b mod m — if m ∣ (a−b).
Example: In class we said that 19≡2 mod 17, that 51 ≡ 0 mod 17, and that 10≡−10mod20.
Proof:
To prove the reflexive property, note that a ≡ a mod m just means that we want to
verify m∣(a−a)=0. We saw a while back, though, that any integer m divides 0, so this
statement is valid.
Finally, for transitivity we are supposed to assume that a ≡ b mod m and b ≡ c mod m, and
somehow conclude that a ≡ c mod m. To prove this result, we note that the first two
congruence conditions tells us that m∣(a−b) and m∣(b−c). Our result on divisibility of integral
linear combinations, then, tells us that m∣(a−b)+(b−c)=a−c. Hence the definition of modular
congruence tells us that a ≡ c mod m.
The benefit of showing that modular congruence is an equivalence relation is that this tells
us that congruence class partition the integers into distinct sets. For instance, when the
modulus is 3, we know that every integer fits into one of the three collections
Coming up with a collection of integers which represent all these possible classes, then, is
an important task. This leads to the following
That is, it is the multiplicative inverse in the ring of integers modulo m. This is equivalent to
The multiplicative inverse of a modulo m exists if and only if a and m are coprime (i.e., if gcd(a, m) = 1).
If the modular multiplicative inverse ofa modulo m exists, the operation of division by a modulo m can be
defined as multiplying by the inverse, which is in essence the same concept as division in the field of
reals.
The modular multiplicative inverse of a modulo m can be found with the extended Euclidean algorithm.
where a, b are given and x, y, and gcd(a, b) are the integers that the algorithm discovers. So, since the
modular multiplicative inverse is the solution to
by the definition of congruence, m | ax − 1, which means that m is a divisor of ax − 1. This, in turn, means
that
Rearranging produces
with a and m given, x the inverse, and q an integer multiple that will be discarded. This is the exact form
of equation that the extended Euclidean algorithm solves—the only difference being that gcd(a, m) = 1 is
predetermined instead of discovered. Thus, a needs to be co-prime to the modulus, or the inverse won't
exist. The inverse is x, and q is discarded.
Number Theory Part 2 12
SUST Programming Camp 2012
int d;
d=Extended_Euclidean(b,a%b,y,x);
y=y-(a/b)*x;
return d;
}
int Inverse_Modulo(int a,int m)
{
int x,y,d;
d=Extended_Euclidean(a,m,x,y);
That is, it is the multiplicative inverse in the ring of integers modulo m. This is equivalent to
The multiplicative inverse of a modulo m exists if and only if a and m are coprime (i.e., if gcd(a,
m) = 1). Let’s see, Calculate Modular Multiplicative Inverse using Fermat Little Theorem:
Fermat’s little theorem states that if m is a prime and a is an integer co-prime to m, then:
am-1 ≡ 1 (mod m)
a-1am-1 ≡ a-1 (mod m)
am-2 ≡ a-1 (mod m)
How Can we calculate the value of (am-2 mod m) ? Using Big Mod Algorithm: Here is C++ Code:
Gauss's generalization
The following is a stronger generalization of Wilson's theorem, due to Carl Friedrich Gauss:
Divisibility Properties
Definition:. d | n means there is an integer k such that n = dk.
2. d | n and n | m =⇒ d | m (transitivity)
4. d | n =⇒ ad | an (multiplication property)
Number Theory Part 2 15
SUST Programming Camp 2012
Number Theory Part 2 16
SUST Programming Camp 2012
Number Theory Part 2 17
SUST Programming Camp 2012
Number Theory Part 2 18
SUST Programming Camp 2012
Type 1: each box costs c1 Taka and can hold exactly n1 marbles
Type 2: each box costs c2 Taka and can hold exactly n2 marbles
I want each of the used boxes to be filled to its capacity and also to minimize the total cost of
buying them. Since I find it difficult for me to figure out how to distribute my marbles among the boxes, I
seek your help. I want your program to be efficient also.
Input
The input file may contain multiple test cases. Each test case begins with a line containing the
integer n (1 <= n <= 2,000,000,000). The second line contains c1 and n1, and the third line
Number Theory Part 2 19
SUST Programming Camp 2012
contains c2 and n2. Here, c1, c2, n1and n2 are all positive integers having values smaller than
2,000,000,000.
A test case containing a zero for n in the first line terminates the input.
Output
For each test case in the input print a line containing the minimum cost solution (two
nonnegative integers m1 and m2, where mi = number of Type i boxes required) if one exists, print "failed"
otherwise.
Solution:
n1m1 + n2m2 = n –---------------------------- (1)
c = c1m1 + c2m2
= c1m1'' +c2m2'' + (c1n2/g – c2n1/g) t
= a + bt
As this is a linear function, its minimum value will be on
either of the boundaries.
Number Theory Part 2 20
SUST Programming Camp 2012
This equation either has d distinct solutions modulo n, where d = gcd(a, n), or it has no
solutions.
Let d = gcd(a, n) and suppose that d = ax' + ny' for some integers x' and y'. If d | b, then
the equation ax = b (mod n) has one of its solutions the value x0, where x0 = x'(b/d) mod n
Algorithm:
MODULAR-LINEAR-EQUATION-SOLVER(a, b, n)
(d, x', y') = EXTENDED-EUCLID(a, n)
if d | b
x0 = x’(b/d) mod n
for i = 0 to d-1
print (x0 + i(n/d)) mod n
else
print “no solutions”
Number Theory Part 2 21
SUST Programming Camp 2012
For i = 1, 2, …. k and for which the ni are pairwise relatively prime, the unique solution of the set
of congruences is –
x = a1 b1 (N/n1) + …. + ak bk (N/nk) (mod N)
Where N = n1 n2 …. Nk
bi is the Inverse of M.
Number Theory Part 2 22
SUST Programming Camp 2012
Practice Problems:
LOJ: 1054, 1067, 1102, 1306, 1007, 1138, 1259, 1278, 1289, 1319, 1306
UVa: 10236, 10329, 10680, 10789, 10924, 10948, 11191, 11347, 11408, 11440, 10090,
TJU: 3618, 1233, 1375, 1476, 1528, 1637, 1698, 1730, 1748, 1991, 2218, 2308, 2502, 2520,
2526, 2601, 2648, 2658, 2859, 2901, 2916, 3043, 3076, 3150, 3232, 3237, 3238, 3259, 3261,
3262, 3288, 3293, 3467, 3483, 3496, 3599,3618
Prepared By: