基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
 收藏
 关注
给出1个正整数N,检测N是否为质数。如果是,输出"Yes",否则输出"No"。

Input
输入一个数N(2 <= N <= 10^30)
Output
如果N为质数,输出"Yes",否则输出"No"。
Input示例
17
Output示例
Yes

大数的素数测试,套上模板

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

51nod 1186 质数检测 V2的更多相关文章

  1. 51nod 1106 质数检测——Mr判素数

    质数检测一般都是根号n的写法 当然Mr判素数的方法可以实现log的复杂度2333 Mr判素数的话 我们根据费马小定理只要P是素数 那么另一个素数x 满足 x^P-1≡1(mod P) 同时 x^2%P ...

  2. (数论 欧拉筛法)51NOD 1106 质数检测

    给出N个正整数,检测每个数是否为质数.如果是,输出"Yes",否则输出"No".   Input 第1行:一个数N,表示正整数的数量.(1 <= N &l ...

  3. F - 质数检测 V2

    https://vjudge.net/contest/218366 Java解 import java.math.BigInteger; import java.util.Scanner; publi ...

  4. 51nod 1106 质数检测

    #include <bits/stdc++.h> using namespace std; int n; ; bool s[maxn]; void is_prime() { memset( ...

  5. 51nod 1181 质数中的质数(质数筛法)

    题目链接:51nod 1181 质数中的质数(质数筛法) #include<cstdio> #include<cmath> #include<cstring> #i ...

  6. 51Nod 1016 水仙花数 V2(组合数学,枚举打表法)

    1016 水仙花数 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 160         难度:6级算法题                水仙花数是指一个 n 位数 ( n≥3 ) ...

  7. 51nod 1022 石子归并 V2 —— DP四边形不等式优化

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1022 1022 石子归并 V2  基准时间限制:1 秒 空间限 ...

  8. 51Nod:1086背包问题 V2

    1086 背包问题 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里 ...

  9. 51nod 1181 质数中的质数

    1181 质数中的质数(质数筛法) 题目来源: Sgu 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 如果一个质数,在质数列表中的编号也是质数,那么就称 ...

随机推荐

  1. IPTABLES基本例子

    iptables –F #删除已经存在的规则 iptables -P INPUT DROP #配置默认的拒绝规则.基本规则是:先拒绝所有的服务,然后根据需要再添加新的规则. iptables -A I ...

  2. Linux Shell_test

    test: 测试Shell脚本里的条件,通过推出状态返回其结果.用法:    test [ expression ] 或 [ [ expression ] ]    注意空格test表达式:是则为真  ...

  3. 在CentOS上把PHP从5.4升级到5.5

    在CentOS上把PHP从5.4升级到5.5 摘要:本文记录了在CentOS 6.3上,把PHP从5.4.8升级到5.5.13的过程. 1. 概述 在我做的一个项目中,最近我对生产服务器上的一系列系统 ...

  4. 【iOS-Tips】-工具Tip

    [iOS-Tips]-工具Tip 1.Xcode自带头文件的路径 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulato ...

  5. HttpWebRequest中的ContentType详解

    1.参考网络资源: http://blog.csdn.net/blueheart20/article/details/45174399  ContentType详解 http://www.tuicoo ...

  6. Statement 与 PreparedStatement 区别

    Statement由方法createStatement()创建,该对象用于发送简单的SQL语句 PreparedStatement由方法prepareStatement()创建,该对象用于发送带有一个 ...

  7. jdbc 操作步骤

    package org.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLExcept ...

  8. Lightoj 1014 - Ifter Party

    I have an Ifter party at the 5th day of Ramadan for the contestants. For this reason I have invited  ...

  9. mac多线程下载神器

    本文参考:https://blog.csdn.net/orangleliu/article/details/46834429 神器:axel 安装(已经安装homebrew前提下,没有请参考:http ...

  10. WebDriverWait显示等待

    等待页面加载完成,找到某个条件发生后再继续执行后续代码,如果超过设置时间检测不到则抛出异常 WebDriverWait(driver, timeout, poll_frequency=0.5, ign ...