题目地址:http://acm.nefu.edu.cn/test/problemshow.php?problem_id=120
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef long long inta;
inta multi(inta a,inta b, inta m)
{
inta rel=0;
while(b>0)
{
if(b&1)
{
rel=(rel+a)%m;
}
b>>=1;
a=(a<<1)%m;
}
return rel;
}
int test(int n)
{
int count=0;
while(n>0)
{n>>=1;
count++;
}
return count;
}
int main()
{
int size=0;
cin>>size;
int p;
while(cin>>p)
{
if(p<=3)
cout<<"yes"<<endl;
else
{
inta mp=1;
mp<<=p;
mp-=1;
inta r=4;
for(int i=0;i<p-2;i++)
{
r=(multi(r,r,mp)-2)%mp;
}
if(r==0) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
}
}
注意要点
1 梅森素数判断 用卢卡斯 莱默判别法
2 long long 进行乘法时 直接乘会溢出 需要转化成加法 边加边取模
3 使用位运算加法 这样算法复杂度为O(log2(n)) 才不会超时