为什么这份代码总是RE
查看原帖
为什么这份代码总是RE
241817
Chancylaser楼主2022/10/27 13:16

rt

#include<iostream>
#include<cstdio>
using namespace std;
typedef unsigned long long LL;
LL pl[10000]={2,3,5,11,37,67,73,97};
LL t;
LL a[12];
LL ksc(LL x,LL y,LL n){
	if (y==0) return 0;
	LL z=ksc(x,y/2,n);
	z=(z+z)%n;
	if (y%2==1) z=(z+x)%n;
	return z;
}
LL ksm(LL x,LL y,LL m){
    LL ans=1;
	LL cnt=x;
    while(y){
        if(y&1){
            ans=ksc(ans,cnt,m);
        }
        cnt=ksc(cnt,cnt,m);
        y>>=1;
    }
    return ans;
}
bool rabin(LL n,int a){
	LL d=n-1;
	int r=0;
	while(d%2==0){
		d=d/2;
		r++;
	}
	LL x=ksm(a,d,n);
	if(x==1)
		return true;
	for(int i=0;i<r;i++){
		if(x==n-1) return true;
		x=ksc(x,x,n);
	}
	return false;
}
bool pr(LL n){
	if(n<2)
		return false;
	for(int a=0;a<8;a++){
		if(n==pl[a]) return true;
		if(n%pl[a]==0) return false;
		if(rabin(n,pl[a])==false) return false;
	}
	return true;
}
int main()
{
	cin>>t;
	for(int i=1;i<=t;i++){
		cin>>a[i];
		if(pr(a[i])==true)
			cout<<"YES\n";
		else 
			cout<<"NO\n";
	}
	return 0;
}
2022/10/27 13:16
加载中...