/*
直接费马小定理
*/
#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. C#面试-关于const和readonly(看了一个点赞很多的帖子有感而发!)

    前景提要: 最近大家都在面试,讨论最多.最基础的问题,莫过于“关于const和readonly常见的笔试题剖析”,等等的大牛解析.我就是一个小菜,只不过,有点不敢苟同大牛的意见.废话少说,进入重点. ...

  2. 为iOS 7而开发 并支持iOS 6

    除了写这本“Developing an iOS 7 Edge”书之外,我还针对iOS 7更新了app,所以我想我应该和大家分享一下我的收获.如果你正在面向iOS 7系统更新应用,同时你的应用还支持iO ...

  3. cloudera目录位置

    http://www.aboutyun.com/thread-9189-1-1.html 这里来的嘿嘿. 1. 相关目录/var/log/cloudera-scm-installer : 安装日志目录 ...

  4. Javascript Framework Scenario

    Mobile----- zepto, jQuery Scroll------ iscrollhttp://cubiq.org/iscroll-4 jo.js widget----- jquery pl ...

  5. mysql数据库操作语句大全

    一 . 常用mysql命令行命令 1 .启动MYSQL服务  net start mysql 停止MYSQL服务  net stop mysql 2 . netstat –na | findstr 3 ...

  6. 几种任务调度的 Java 实现方法与比较Timer,ScheduledExecutor,Quartz,JCronTab

    几种任务调度的 Java 实现方法与比较 综观目前的 Web 应用,多数应用都具备任务调度的功能.本文由浅入深介绍了几种任务调度的 Java 实现方法,包括 Timer,Scheduler, Quar ...

  7. Majority Element II——LeetCode

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. 自己做的网页页面导航浏览JS/JQuery_版本2(优化边缘)

    版本2增加了宽宽的边界,边界内鼠标也可以导航.边界对应这HTML页面的边界.目前右下角有时会导致功能失效.版本1. 这次找了个更好的例子,实践中产生这个需求的真实例子,点我Demo. 需求: 版本1: ...

  9. C字符数组及其应用

    1.字符数组和其他数值类型的数组的定义引用和初始化都是相同的. 特别注意的是: 在C语言中没有专门的字符串变量,通常用一个字符数组来存放一个字符串. \0'是由C编译系统自动加上的. 2. C语言允许 ...

  10. 暴力求解——素环数 Prime Ring Problem ,UVa 524

    Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers i ...