题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1186

题意:中文题目诶~

思路:miller_rabin模板 (详情可参见: http://blog.csdn.net/s031302306/article/details/51606018)

注意这里的 n 为 1e30,需要用字符串处理

代码:

 #include <string.h>
#include <cstdlib> #define MAXL 4
#define M10 1000000000
#define Z10 9 const int zero[MAXL - ] = {}; struct bnum{
int data[MAXL]; // 断成每截9个长度
// 读取字符串并转存
void read(){
memset(data, , sizeof(data));
char buf[];
scanf("%s", buf);
int len = (int)strlen(buf);
int i = , k;
while (len >= Z10){
for (k = len - Z10; k < len; ++k){
data[i] = data[i] * + buf[k] - '';
}
++i;
len -= Z10;
}
if (len > ){
for (k = ; k < len; ++k){
data[i] = data[i] * + buf[k] - '';
}
}
} bool operator == (const bnum &x){
return memcmp(data, x.data, sizeof(data)) == ;
} bnum & operator = (const int x){
memset(data, , sizeof(data));
data[] = x;
return *this;
} bnum operator + (const bnum &x){
int i, carry = ;
bnum ans;
for (i = ; i < MAXL; ++i){
ans.data[i] = data[i] + x.data[i] + carry;
carry = ans.data[i] / M10;
ans.data[i] %= M10;
}
return ans;
} bnum operator - (const bnum &x){
int i, carry = ;
bnum ans;
for (i = ; i < MAXL; ++i){
ans.data[i] = data[i] - x.data[i] - carry;
if (ans.data[i] < ){
ans.data[i] += M10;
carry = ;
}else{
carry = ;
}
}
return ans;
} // assume *this < x * 2
bnum operator % (const bnum &x){
int i;
for (i = MAXL - ; i >= ; --i){
if (data[i] < x.data[i]){
return *this;
}
else if (data[i] > x.data[i]){
break;
}
}
return ((*this) - x);
} bnum & div2(){
int i, carry = , tmp;
for (i = MAXL - ; i >= ; --i){
tmp = data[i] & ;
data[i] = (data[i] + carry) >> ;
carry = tmp * M10;
}
return *this;
} bool is_odd(){
return (data[] & ) == ;
} bool is_zero(){
for (int i = ; i < MAXL; ++i){
if (data[i]){
return false;
}
}
return true;
}
}; void mulmod(bnum &a0, bnum &b0, bnum &p, bnum &ans){//计算a0*b0%p
ans = ;
bnum tmp = a0, b = b0;
while (!b.is_zero()){
if (b.is_odd()){
ans = (ans + tmp) % p;
}
tmp = (tmp + tmp) % p;
b.div2();
}
} void powmod(bnum &a0, bnum &b0, bnum &p, bnum &ans){//计算a0^b0%p
bnum tmp = a0, b = b0;
ans = ;
while (!b.is_zero()){
if (b.is_odd()){
mulmod(ans, tmp, p, ans);
}
mulmod(tmp, tmp, p, tmp);
b.div2();
}
} bool MillerRabinTest(bnum &p, int iter){
int i, small = , j, d = ;
for (i = ; i < MAXL; ++i){
if (p.data[i]){
break;
}
}
if (i == MAXL){
// small integer test
if (p.data[] < ){
return false;
}
if (p.data[] == ){
return true;
}
small = ;
}
if (!p.is_odd()){
return false; // even number
}
bnum a, s, m, one, pd1;
one = ;
s = pd1 = p - one;
while (!s.is_odd()){
s.div2();
++d;
} for (i = ; i < iter; ++i){
a = rand();
if (small){
a.data[] = a.data[] % (p.data[] - ) + ;
}else{
a.data[] = a.data[] / M10;
a.data[] %= M10;
}
if (a == one){
continue;
} powmod(a, s, p, m); for (j = ; j < d && !(m == one) && !(m == pd1); ++j){
mulmod(m, m, p, m);
}
if (!(m == pd1) && j > ){
return false;
}
}
return true;
} int main(void){
bnum x;
x.read();
puts(MillerRabinTest(x, ) ? "Yes" : "No");
return ;
}

对于long long范围:

 #include <stdio.h>
#include <iostream>
#include <cstdlib>
#define ll long long
using namespace std; //利用二进制计算a*b%mod
ll multi_mod(ll a, ll b, ll mod){
ll ans = 0LL;
a %= mod;
while(b){
if (b & 1LL) ans = (ans+a)%mod;
b >>= 1LL;
a = (a+a)%mod;
}
return ans;
} //计算a^b%mod
ll power_mod(ll a, ll b, ll mod){
ll ans = 1LL;
a %= mod;
while(b){
if(b & 1LL) ans = multi_mod(ans, a, mod);
b >>= 1LL;
a = multi_mod(a, a, mod);
}
return ans;
} //Miller-Rabin测试,测试n是否为素数
bool miller_rabin(ll n, int repeat){
if (2LL == n || 3LL == n) return true;
if (n%== || n%== || n%==) return false; //将n分解为2^s*d
ll d = n - 1LL;
int s = ;
while(!(d & 1LL)){
s++;
d >>= 1LL;
}
// srand((unsigned)time(0));
for(int i=; i<repeat; ++i){//重复repeat次
ll a = rand() % (n - ) + ;//取一个随机数,[2,n-1)
ll x = power_mod(a, d, n);
ll y = 0LL;
for(int j=; j<s; ++j){
y = multi_mod(x, x, n);
if (1LL == y && 1LL != x && n-1LL != x) return false;
x = y;
}
if (1LL != y) return false;
}
return true;
} int main(void){
ll n;
cin >> n;
if(miller_rabin(n, )) cout << "Yes" << endl;
else cout << "No" << endl;
return ;
}

51nod1186(Miller-Rabin)的更多相关文章

  1. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

  2. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

  3. poj 1811 Pallor Rho +Miller Rabin

    /* 题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子 Miller Rabin +Poller Rho 大素数判定+大数找质因子 后面这个算法嘛 基于Birthday Pa ...

  4. POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  5. Miller Rabin算法详解

    何为Miller Rabin算法 首先看一下度娘的解释(如果你懒得读直接跳过就可以反正也没啥乱用:joy:) Miller-Rabin算法是目前主流的基于概率的素数测试算法,在构建密码安全体系中占有重 ...

  6. 与数论的厮守01:素数的测试——Miller Rabin

    看一个数是否为质数,我们通常会用那个O(√N)的算法来做,那个算法叫试除法.然而当这个数非常大的时候,这个高增长率的时间复杂度就不够这个数跑了. 为了解决这个问题,我们先来看看费马小定理:若n为素数, ...

  7. $Miller Rabin$总结

    \(Miller Rabin\)总结: 这是一个很高效的判断质数的方法,可以在用\(O(logn)\) 的复杂度快速判断一个数是否是质数.它运用了费马小定理和二次探测定理这两个筛质数效率极高的方法. ...

  8. 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)

    关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...

  9. POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

    GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...

  10. Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法

    BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1044  Solved: 322[Submit][ ...

随机推荐

  1. iOS 关于NSNotificationCenter

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,  Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification. NSNotificationCen ...

  2. HAOI 2017 游记

    省选 2017年4月23日 流水账式游记,不喜勿喷. Day0: 准备出发,上午敲了一顿板子,板子敲完了就打小游戏,老师也不管了. 过程中各种奶,说什么今年一定考仙人掌啦,今年一定考字符串啦,今年一定 ...

  3. 20145239杜文超 《Java程序设计》第10周学习总结

    20145239 <Java程序设计>第10周学习总结 教材学习内容总结 Java的网络编程 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 网络概述 1.计算机 ...

  4. 微软面试题:鸡蛋从第N层及以上的楼层落下会摔破

    from:https://blog.csdn.net/qq_18425655/article/details/52326709   题目: 有一栋楼共100层,一个鸡蛋从第N层及以上的楼层落下来会摔破 ...

  5. Go丨语言对数据库操作报错 panic: dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it.

    panic: dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine ac ...

  6. linux安装与卸载软件

    在ubuntu系统中,通常使用apt-get命令完成对软件的安装与卸载 安装的软件通常都放置在一些源中,国内有很多镜像源供下载使用,而系统设置的源保存在目录/etc/apt/sources.list文 ...

  7. Linux网络编程 gethostbyaddr()

    C语言函数 概述: 返回对应于给定地址的主机信息. #include <winsock.h> struct hostent FAR *PASCAL FAR gethostbyaddr(co ...

  8. umount 卸载 无响应的 NFS 文件系统

    当NFS Client 无法访问 NFS Server的适合,在Client上df操作等就会挂起. 这个适合需要将挂载的NFS卸载掉.在不知道挂载点的情况下,可以使用nfsstat -m 命令来查看. ...

  9. 发挥到极致的Asterisk SS7 解决方案【转】

    基于SS7的开源解决方案在国内已经安装了很多.很多用户都使用chan_ss7 开源协议栈作为呼叫中心,400电话,计费结算的系统.随着国内对开源Asterisk的认可程度越来越高. Asterisk让 ...

  10. CodeForces Div1: 995 D. Game(数学期望)

    Allen and Bessie are playing a simple number game. They both know a function f:{0,1}n→Rf:{0,1}n→R, i ...