标签:long bin puts i++ for ons a* last put
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
const int times=100;
long long n;
__int128 ksm(__int128 a,__int128 b,__int128 m){
__int128 res=1;
while(b){
if(b&1)
res=(res*a)%m;
a=(a*a)%m,b>>=1;
}
return res%m;
}
__int128 miller_rabin(long long x){
if(x<2)
return 0;
if((x&1)==0)
return x>2? 0:1;
__int128 y=x-1,cnt=0,num,last,i,j;
while((y&1)==0)
y>>=1,cnt++;
for(i=1;i<=times;i++){
num=ksm(rand()%(x-1)+1,y,x),last=num;
for(j=1;j<=cnt;j++){
num=num*num%n;
if(last!=1&&last!=x-1&&num==1)
return 0;
last=num;
}
if(num!=1)
return 0;
}
return 1;
}
int main(){
srand(time(0));
while(~scanf("%lld",&n))
puts(miller_rabin(n)==0? "N":"Y");
return 0;
}
标签:long bin puts i++ for ons a* last put
原文地址:https://www.cnblogs.com/xiaoziyao/p/11862637.html