Board A (Extended Euclid)
Title Description
Seeking on x congruence equations A x ≡ . 1 ( m O D B the smallest positive integer) solution.
Input Format
Line, comprising two positive integers A, B , separated by a space.
Output Format
A positive integer X , i.e., the smallest positive integer solutions. Input data guaranteed solvable.
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 using namespace std; 5 long long xx,yy; 6 void exgcd(long long aa,long long bb) 7 { 8 if(bb==0) 9 { 10 xx=1; 11 yy=0; 12 return; 13 } 14 exgcd(bb,aa%bb); 15 long long tt=yy; 16 yy=xx-(aa/bb)*yy; 17 xx=tt; 18 } 19 int main() 20 { 21 long long aa,bb; 22 scanf("%lld%lld",&aa,&bb); 23 exgcd(aa,bb); 24 xx=((xx%bb)+bb)%bb; 25 printf("%lld",xx); 26 return 0; 27 }
Board B1 (multiplicative inverse (Fermat's little theorem or Euler's theorem))
Title Description
Given n, p 1 ~ n find all integers in the multiplicative inverse modulo p sense.
Input Format
Line n, p
Output Format
n rows, i represents the i-th row in the inverse sense modulo p.
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 using namespace std; 5 long long mod; 6 long long ksm(long long aa,long long bb) 7 { 8 long long ret=1; 9 while(bb!=0) 10 { 11 if(bb%2==1)ret=((ret%mod)*(aa%mod))%mod; 12 bb/=2; 13 aa=(aa%mod)*(aa%mod)%mod; 14 } 15 return ret%mod; 16 } 17 int main() 18 { 19 long long i; 20 long long n; 21 scanf("%lld%lld",&n,&mod); 22 for(i=1;i<=n;i++) 23 { 24 printf("%lld\n",ksm(i,mod-2 )); 25 } 26 return 0 ; 27 }
Board B2 (multiplicative inverse (extended Euclidean))
Title Description
Given n, p 1 ~ n find all integers in the multiplicative inverse modulo p sense.
Input Format
Line n, p
Output Format
n rows, i represents the i-th row in the inverse sense modulo p.
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 using namespace std; 5 long long n,mod; 6 long long xx,yy; 7 void exgcd(long long aa,long long bb) 8 { 9 if(bb==0) 10 { 11 xx=1; 12 yy=0; 13 return; 14 } 15 exgcd(bb,aa%bb); 16 long long tt=yy; 17 yy=xx-(aa/bb)*yy; 18 xx=tt; 19 } 20 int main() 21 { 22 long long i; 23 scanf("%lld%lld",&n,&mod); 24 for(i=1;i<=n;i++) 25 { 26 exgcd(i,mod); 27 xx=((xx%mod)+mod)%mod; 28 printf("%lld\n",xx); 29 } 30 return 0; 31 }
Board B3 (multiplicative inverse (linear sieve))
Title Description
Given n, p 1 ~ n find all integers in the multiplicative inverse modulo p sense.
Input Format
Line n, p
Output Format
n rows, i represents the i-th row in the inverse sense modulo p.
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 using namespace std; 5 long long inv[3000100]; 6 long long n,mod; 7 int main() 8 { 9 scanf("%lld%lld",&n,&mod); 10 long long i; 11 inv[1]=1; 12 printf("1\n"); 13 for(i=2;i<=n;i++) 14 { 15 inv[i]=mod-(mod/i)*inv[mod%i]%mod; 16 printf("%lld\n",inv[i]); 17 } 18 return 0; 19 }
Board C (linear prime number mesh)
Title Description
As stated, a given range N, you need to handle a number of M asking whether prime number (per figures are within the range 1-N)
Input Format
The first line contains two positive integers N, M, respectively, represents the number of range queries and queries.
Next M lines contains an integer not less than 1 and not greater than N, i.e. the number of interrogation is prime.
Output Format
Output comprising M rows, each behavior Yes or No, i.e., the results of each sequence as a query.
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 using namespace std; 5 bool vist[10000100]; 6 int prim[1000010]; 7 int tot=0; 8 int main() 9 { 10 int n,m; 11 int i,j; 12 scanf("%d%d",&n,&m); 13 for(i=2;i<=n;i++) 14 { 15 if(!vist[i])prim[++tot]=i; 16 for(j=1;j<=tot&&i*prim[j]<=n;j++) 17 { 18 vist[prim[j]*i]=1; 19 if(i%prim[j]==0) 20 break; 21 } 22 } 23 int qq; 24 vist[1]=1; 25 for(i=1;i<=m;i++) 26 { 27 scanf("%d",&qq); 28 if(vist[qq]==0)printf("Yes\n"); 29 else printf("No\n"); 30 } 31 return 0; 32 }
Board D (GCD & LCM)
Title Description
Enter two positive integers m andn, and seeking the least common multiple of the greatest common divisor.
Input Format
Two integers m andn。
Output Format
The greatest common divisor of two integers, least common multiple
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 using namespace std; 5 long long gcd(long long aa,long long bb) 6 { 7 if(bb==0)return aa; 8 else return gcd(bb,aa%bb); 9 } 10 int main() 11 { 12 long long n,m; 13 scanf("%lld%lld",&n,&m); 14 long long ls=gcd(n,m); 15 printf("%lld %lld",ls,n*m/ls); 16 return 0; 17 }