/*
直接费马小定理
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#define ll long long
using namespace std;
ll slow_mul(ll a,ll b,ll c)
{
ll ans=;
a=a%c;b=b%c;
while(b)
{
if(b&)
{
b--;
ans+=a;
ans%=c;
}
a<<=;a%=c;b>>=;
}
return ans;
}
ll Mi(ll p,ll a,ll mod)
{
if(p==)return ;
ll x=Mi(p/,a,mod)%mod;
x=slow_mul(x,x,mod);
if(p%==)x=slow_mul(x,a,mod);
return x;
}
int main()
{
srand(unsigned(time()));
ll p,d,a;
cin>>p;
int falg=;
if(p==)
{
printf("Yes");
return ;
}
if(p==)
{
printf("No");
return ;
}
for(int i=;i<=;i++)
{
a=rand()%(p-)+;
d=Mi(p-,a,p);
if(d!=)
{
falg=;
break;
}
else falg=;
}
if(falg==)printf("Yes");
else printf("No");
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#define ll long long
#define T 10
using namespace std;
ll slow_mul(ll a,ll b,ll c)//防止爆掉
{
ll ans=;
a=a%c;b=b%c;
while(b)
{
if(b&)
{
b--;ans+=a;
ans=ans%c;
}
a=a<<;a=a%c;
b=b>>;
}
return ans;
}
ll Mi(ll a,ll m,ll n)//快速幂 a^m%n
{
if(m==)return ;
ll x=Mi(a,m/,n)%n;
x=slow_mul(x,x,n);
if(m%==)x=slow_mul(x,a,n);
return x;
}
bool Miller_Rabin(ll n)
{
if(n<)return ;
if(n==)return ;
if(n%==)return ;
ll m=n-,j=;
while(m%==)//计算m j 使得n-1=m*2^j且j尽量大
{
j++;
m >>=;
}
srand(unsigned(time()));
for(int i=;i<=T;i++)//T次测试
{
ll a=rand()%(n-)+;
ll x=Mi(a,m,n);//计算a^m%n
ll y;
for(int k=;k<=j;k++)
{
y=slow_mul(x,x,n);
if(y==&&x!=&&x!=n-)return ;//一定不是素数
x=y;
}
if(x!=)return ;//不符合费马小定理
}
return ;
}
int main()
{
ll n;
cin>>n;
if(Miller_Rabin(n))printf("Yes\n");
else printf("No\n");
}

Miller_Rabin codevs 1702 素数判定2的更多相关文章

  1. Miller-Rabin算法 codevs 1702 素数判定 2

    转载自:http://www.dxmtb.com/blog/miller-rabbin/ 普通的素数测试我们有O(√ n)的试除算法.事实上,我们有O(slog³n)的算法. 定理一:假如p是质数,且 ...

  2. Codevs 1702 素数判定 2(Fermat定理)

    1702 素数判定 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 一个数,他是素数么? 设他为P满足(P< ...

  3. codevs 1702素数判定2

    Miller-Rabin算法实现,但是一直被判题程序搞,输入9999999999得到的结果分明是正确的但是一直说我错 #include <cstdio> #include <cmat ...

  4. codevs——1430 素数判定

    1430 素数判定  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 青铜 Bronze 题解       题目描述 Description 质数又称素数.指在一个大于1的自然数中, ...

  5. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  6. 10^9以上素数判定,Miller_Rabin算法

    #include<iostream> #include<cstdio> #include<ctime> #include<string.h> #incl ...

  7. Miller_Rabin()算法素数判定 +ollard_rho 算法进行质因数分解

    //****************************************************************// Miller_Rabin 算法进行素数测试//速度快,而且可以 ...

  8. 【数论】【素数判定】CODEVS 2851 菜菜买气球

    素数判定模板. #include<cstdio> #include<map> using namespace std; ],ans=-,l,r,n,sum[]; bool is ...

  9. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

随机推荐

  1. MySQL账号授权操作

    Mysql权限控制 - 允许用户远程连接 设置mysql root密码: mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = P ...

  2. python之加密

    import hashlib obj = hashlib.md5(bytes('adfasfasdfsfasf',encoding = 'utf-8')) obj.update(bytes('123' ...

  3. Intrusion Detection of Specific Area Based on Video

  4. [POJ 1742] Coins 【DP】

    题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 D ...

  5. 电容式触摸控制器PCB布局

    在目前市场上可提供的PCB(印刷电路板)基材中,FR4是最常用的一种.FR4是一种玻璃纤维增强型环氧树脂层压板,PCB可以是单层或多层. 在触摸模块的尺寸受限的情况下,使用单层PCB不是总能行得通的, ...

  6. nodejs触发事件的两种方式

    nodejs触发事件的两种方式: 方式之一:通过实例化events.EventEmitter //引入events模块 var events = require('events'); //初始化eve ...

  7. Cash Machine(多重背包)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24067   Accepted: 8414 Description A Ba ...

  8. 【转】 为SeekBar滑块设置固定值以及自定义Seekbar,progressbar样式--不错

    原文网址:http://blog.csdn.net/jdsjlzx/article/details/7804080 最近在项目中使用到了seekbar和progressbar,且必须按照设计要求来进行 ...

  9. Sqrt(x)——LeetCode

    Implement int sqrt(int x). Compute and return the square root of x. 题目大意:实现求一个int的根. 解题思路:二分. public ...

  10. hdu 1175 连连看 DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路 ...