您的位置:首页 > 其它

1702 素数判定 2

2016-07-10 20:38 330 查看
1702 素数判定 2

时间限制: 1 s

空间限制: 128000 KB

题目等级 : 钻石 Diamond

题目描述 Description

一个数,他是素数么?

设他为P满足(P<=263-1)

输入描述 Input Description

P

输出描述 Output Description

Yes|No

样例输入 Sample Input

2

样例输出 Sample Output

Yes

数据范围及提示 Data Size & Hint

算法导论——数论那一节

注意Carmichael Number

题目数据太大,筛法绝对爆,所以就用费马小定理,简单搞定。

#include<cstdio>
#include<iostream>
using namespace std;
typedef unsigned long long ll;
int pp[]={2,3,5,7,11,13,17,19,23,29};
ll p;
/*慢速乘法*/
ll mul(ll a,ll b){
ll ans=0;
for(ll i=a;i;i>>=1){
if(i&1) ans=(ans+b)%p;
b=(b+b)%p;
}
return ans%p;
}
/*快速幂*/
ll mull(ll a,ll b){
ll ans=1;
for(ll i=a;i;i>>=1){
if(i&1) ans=mul(ans,b)%p;
b=mul(b,b)%p;
}
return ans%p;
}
bool check(){
if(p<2||p&1==0) return 0;
if(p==2) return 1;
for(ll i=0;i<10;i++){
if(mull(p-1,pp[i])!=1)
return 0;
}
return 1;
}
int main()
{
std::ios::sync_with_stdio(false);
cin>>p;
if(check()) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: