题目链接: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. 【ELK】Elasticsearch的备份和恢复

    非原创,只是留作自己查询使用,转自http://keenwon.com/1393.html Elasticsearch的备份和恢复 备份 Elasticsearch的一大特点就是使用简单,api也比较 ...

  2. python获取当前的时间

    打印出当前的年月日时分秒 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) 2018-09-05 09:39: ...

  3. 微信小程序开发:学习笔记[5]——JavaScript脚本

    微信小程序开发:学习笔记[5]——JavaScript脚本 快速开始 介绍 小程序的主要开发语言是 JavaScript ,开发者使用 JavaScript 来开发业务逻辑以及调用小程序的 API 来 ...

  4. BZOJ 4435 [Cerc2015]Juice Junctions 分治最小割+hash

    分治最小割的题目,要求n2. 之前用的n3的方法自然不能用了. 于是用hash,设hash[i][j]表示在最小割为i的时候,j是否与S联通. 看懂这个需要理解一下最小割树的构造. 这种题建议用EK写 ...

  5. Python多人聊天室

    一.目的 以实现小项目的方式,来巩固之前学过的Python基本语法以及相关的知识. 二.相关技术: 1.wxpython GUI编程 2.网络编程 3.多线程编程 4.数据库编程 5.简单的将数据导出 ...

  6. excel根据数据源变化的动态图表

    http://www.excelhome.net/lesson/article/excel/1798.html 这个链接讲的可以.

  7. 纯CSS3实现淡入淡出下拉菜单

    纯CSS3实现淡入淡出下拉菜单是一款比较简单清新的CSS3教程下拉菜单,这款下拉菜单是垂直方向的,点击主菜单项可以展开和折叠子菜单,在展开折叠的过程中伴随着淡入淡出的动画效果 源代码:http://w ...

  8. 企业安全建设之搭建开源SIEM平台(上)

    前言 SIEM(security information and event management),顾名思义就是针对安全信息和事件的管理系统,针对大多数企业是不便宜的安全系统,本文结合作者的经验介绍 ...

  9. 【Codeforces】Gym 101156G Non-Attacking Queens 打表

    题意 求$n\times n$的棋盘上放$3$个皇后使得互相不攻击的方案数 拓展是$m\times n$棋盘上放$k$皇后,暴力打表找到了公式 OEIS 代码 import java.math.Big ...

  10. L84

    Hospital Noise May Disrupt Patient Improvement Many who need restorative rest the most might not be ...