/*
直接费马小定理
*/
#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. BAE 环境下配置 struts2 + spring + hibernate(SSH)(二)struts2

    在myeclipse下开发的 应用但是 放到BAE下就出现了问题,虽然显示发布成功,但是访问的时候就会出现503 Service Unavailable 错误.通过调整 web.xml 发现纯Serv ...

  2. vs 2015 菜单重复的问题解决方法

    打开 “运行” 输入 D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe /resetuserdata ...

  3. c++ 顺序容器学习 - 容器适配器

    摘要: 对 容器适配器 的疑问. 刚开始接触 容器适配器 时,总感觉怪怪的,认为多此一举,顺手搜了搜,原来我在这一点is not alone: STL容器适配器的用途 其中有个老兄说的好,这里 引用一 ...

  4. net programming guid

    Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...

  5. 【转】nodejs

    Node.JS + MongoDB技术讲座            云计算 + 大数据 = 未来. 在中国的云计算上基本上是一个概念,个人感觉与当初的SOA没有太大的区别,空泛的理论. 中小型开发的未来 ...

  6. "Ray, Pass me the dishes!"

    uvaLive3938:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...

  7. UBUNTU下FPT工具--lftp使用说明

    lftp 是UBUNTU下一个功能强大的下载工具,它支持访问文件的协议: ftp, ftps, http, https, hftp, fish.(其中ftps 和https需要在编译的时候包含open ...

  8. MySql的卸载问题

    windows下mysql的卸载: 彻底卸载Mysql的方法:   (1),先在服务(开始——>控制面板——>管理工具——>服务)里停掉MySQL的服务.打开控制面板-添加删除程序, ...

  9. 快速创建php server

    mkdir testmv test.html testphp -S localhost:8082http://localhost:8082/test.html

  10. Light OJ 1018 - Brush (IV)

    题目大意:     一个二维平面上有N个点,一把刷子,刷一次可以把一条线上的所有点都刷掉.问最少刷多少次,可以把全部的点都刷完 状态压缩DP, 用记忆化搜索来写, 需要有个优化不然会超时. ===== ...