4, Number Theory

4.1 Finally, non-factorial 0

// factorial last nonzero bits, the complexity O (nlogn)
 // return the care, n as a string passed 
#include < String .h>
 #define MAXN 10000
 int lastdigit ( char * buf) {
 const  int MOD [ 20 ] = { 1 , 1 , 2 , 6 , 4 , 2 , 2 , 4 , 2 , 8 , 4 , 4 , 8 , 4 , 6 , 8 , 8 ,6,8,2};
int len=strlen(buf),a[MAXN],i,c,ret=1;
if (len==1)
return mod[buf[0]-'0'];
for (i=0;i<len;i++)
a[i]=buf[len-1-i]-'0';
for (;len;len-=!a[len-1]){
ret=ret*mod[a[1]%2*10+a[0]]%5;
for (c=0,i=len-1;i>=0;i--)
67
c=c*10+a[i],a[i]=c/5,c%=5;
}
return ret+ret%2*5;
}

Mode of linear equations 4.2

#ifdef WIN32
typedef __int64 i64;
#else
typedef long long i64;
#endif
//扩展 Euclid 求解 gcd(a,b)=ax+by
int ext_gcd(int a,int b,int& x,int& y){
int t,ret;
if (!b){
x=1,y=0;
return a;
}
ret=ext_gcd(b,a%b,x,y);
t=x,x=y,y=t-a/b*y;
return ret;
}
//Calculated m ^ a, O (loga) , itself useless, note that according to the method of the bit processing :-P 
int exponent The ( int m, int A) {
 int RET = . 1 ;
 for (; A; A = >> . 1 , * = m m)
 IF (A & . 1 ) 
RET * = m;
 return RET; 
} 
// computing power modulo n-MOD B ^ A, O (logb) 
int modular_exponent ( int A, int B, int n-) { // ^ n-B MOD A 
int RET = . 1 ;
 for (; B; B >> = . 1 , A = ( int) ((I64) A) * A% n-)
 IF (B & . 1 ) 
RET = ( int ) ((I64) RET) * A% n-;
 return RET; 
} 
// solving mode linear equation ax = b (mod n)
 // returns the number of solutions, the solution is stored in Sol [] in 
68 
// required n> 0, the range of solutions. 1-0..n 
int modular_linear ( int a, int B, int n-, int * Sol) {
 int D, E, X, Y, I; 
D = ext_gcd (A, n-, X, Y);
 IF (B% D)
 return  0 ; 
E = (X * (B / D)% n-+ n-)% n-;
for (I = 0 ; I <D; I ++ ) 
Sol [I] = (E + I * (n-/ D))% n-;
 return D; 
} 
// solving mode linear equations (Chinese Remainder Theorem)
 // X B = [0] (MOD W [0])
 // X = B [. 1] (MOD W [. 1])
 // ...
 // X = B [-K. 1] (MOD W [-K. 1] )
 // required w [i]> 0, w [i] and w [j] coprime Solutions range 1..n, n = w [0] * w [1] * ... * w [k -1] 
int modular_linear_system ( int B [], int W [], int K) {
 int D, X, Y, A = 0 , m, n-= . 1 , I;
 for (I = 0 ; I <K; I ++ ) 
the n-*=w[i];
for (i=0;i<k;i++){
m=n/w[i];
d=ext_gcd(w[i],m,x,y);
a=(a+y*m*b[i])%n;
}
return (a+n)%n;
}

4.3 prime

//用素数表判定素数,先调用 initprime
int plist[10000],pcount=0;
int prime(int n){
int i;
if ((n!=2&&!(n%2))||(n!=3&&!(n%3))||(n!=5&&!(n%5))||(n!=7&&!(n%7)))
return 0;
for (i=0;plist[i]*plist[i]<=n;i++)
if (!(n%plist[i]))
return 0;
return n>. 1 ; 
} 
69 
void initprime () {
 int I;
 for (the plist [Pcount ++] = 2 , I = . 3 ; I < 50000 ; I ++ )
 IF (Prime (I)) 
the plist [Pcount ++] = I; 
} 
// Rabin Miller
 // determines whether n is a natural number prime
 // Time lower the higher the probability of failure, typically take 10 to 50 
#include <stdlib.h> 
#ifdef the WIN32 
typedef the __int64 I64; 
#else 
typedef Long  Long I64;
 #endif 
int modular_exponent ( int A, int B,int n){ //a^b mod n
int ret;
for (;b;b>>=1,a=(int)((i64)a)*a%n)
if (b&1)
ret=(int)((i64)ret)*a%n;
return ret;
}
// Carmicheal number: 561,41041,825265,321197185
int miller_rabin(int n,int time=10){
if (n==1||(n!=2&&!(n%2))||(n!=3&&!(n%3))||(n!=5&&!(n%5))||(n!=7&&!(n%7)))
return 0;
while (time--)
if
(modular_exponent(((rand()&0x7fff<<16)+rand()&0x7fff+rand()&0x7fff)%(n-1)+1,n-1,n)!=1)
return 0;
return 1;
}

4.4 Euler function

int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
inline int lcm(int a,int b){
return a/gcd(a,b)*b;
}
//求 1..n-1 中与 n 互质的数的个数
int eular(int n){
int ret=1,i;
for (i=2;i*i<=n;i++)
if (n%i==0){
n/=i,ret*=i-1;
while (n%i==0)
n/ = i, right * = i; 
} 
If (n> 1 ) 
right * = n- 1 ;
Return the right; 
}

 

Guess you like

Origin www.cnblogs.com/godoforange/p/11240505.html