标签:cstring string script strong and cst log ber put
一个数,他是素数么?
设他为P满足(P<=263-1)
P
Yes|No
2
Yes
算法导论——数论那一节
注意Carmichael Number
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<ctime> 6 #define ll long long int 7 using namespace std; 8 ll n; 9 ll pd[14]={10,35,77,535,71497,2,3,5,7,11,3161}; 10 ll fastmul(ll a,ll b) 11 { 12 ll r=0; 13 ll base=a; 14 while(b!=0) 15 { 16 if(b%2!=0) 17 { 18 b--; 19 r=(r+base)%n; 20 } 21 b=b/2; 22 base=(base+base)%n; 23 } 24 return r%n; 25 } 26 ll fastpow(ll a,ll b) 27 { 28 ll r=1; 29 ll base=a; 30 while(b!=0) 31 { 32 if(b%2!=0) 33 r=fastmul(r,base)%n; 34 base=fastmul(base,base)%n; 35 b=b/2; 36 } 37 return r%n; 38 } 39 ll check(ll n) 40 { 41 if(n==2) 42 { 43 return 1; 44 } 45 if(n<2&&(n%2==0)) 46 { 47 return 0; 48 } 49 for(ll i=0;i<11;i++) 50 { 51 ll x=pd[i]; 52 if(x%n==0) 53 continue; 54 ll ans=fastpow(x,n-1)%n; 55 if(ans!=1) 56 return 0; 57 } 58 return 1; 59 } 60 int main() 61 { 62 //srand(time(0)); 63 scanf("%I64d",&n); 64 cin>>n; 65 if(check(n)) 66 { 67 printf("Yes"); 68 } 69 else 70 { 71 printf("No"); 72 } 73 return 0; 74 }
标签:cstring string script strong and cst log ber put
原文地址:http://www.cnblogs.com/zwfymqz/p/6744059.html