/*
直接费马小定理
*/
#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. python自动开发之第十八天

    一.JS正则 test - 判断字符串是否符合规定的正则 rep = /\d+/; rep.test("asdfoiklfasdf89asdfasdf") # true rep = ...

  2. Rewrite的QSA是什么意思?

    原版的英文: When the replacement URI contains a query string, the default behavior of RewriteRule is to d ...

  3. C语言学习笔记(二):指针的用法

    与其说指针是一种工具,不如先说指针是一种数据类型. -------------------------------------------------------------华丽的分割线------- ...

  4. MySQL Explain 结果解读与实践

    Explain 结果解读与实践   基于 MySQL 5.0.67 ,存储引擎 MyISAM .   注:单独一行的"%%"及"`"表示分隔内容,就象分开&qu ...

  5. 使用date命令,进行时间戳和日期时间的互转

    首先是知道时间转成时间戳 date -d "2014-01-16 12:30:11" +%s - :: - :: 其次是知道时间戳,想要知道当时的时间 date -d '1970- ...

  6. oracle 对象权限 系统权限 角色权限

    系统权限: 允许用户执行特定的数据库动作,如创建表.创建索引.连接实例等 对象权限: 允许用户操纵一些特定的对象,如读取视图,可更新某些列.执行存储过程等 select * from user_sys ...

  7. BZOJ 3107 二进制a+b

    Description 输入三个整数\(a, b, c\),把它们写成无前导\(0\)的二进制整数.比如\(a=7, b=6, c=9\),写成二进制为\(a=111, b=110, c=1001\) ...

  8. websphere安装和mvn dependency:copy-dependencies

    http://www.blogjava.net/paulwong/archive/2009/09/19/295657.html http://ljhzzyx.blog.163.com/blog/sta ...

  9. backbone初次使用及hello world

    官网:http://backbonejs.org/ Backbone.js gives structure to web applications by providing models with k ...

  10. ikely()与unlikely() 都等同于if, 此处只是做编译优化

    ikely()与unlikely()在2.6内核中,随处可见,那为什么要用它们?它们之间有什么区别呢? 首先明确: if (likely(value))等价于if (value)if (likely( ...