大素数判断(miller-Rabin测试)
2 seconds
256 megabytes
standard input
standard output
PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer n that for each positive integer m number n·m + 1 is a prime number".
Unfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Could you prove it wrong? Write a program that finds a counterexample for any n.
The only number in the input is n (1 ≤ n ≤ 1000) — number from the PolandBall's hypothesis.
Output such m that n·m + 1 is not a prime number. Your answer will be considered correct if you output any suitable m such that 1 ≤ m ≤ 103. It is guaranteed the the answer exists.
3
1
4
2 代码:
#include <iostream>
#include <time.h>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map> #define LL long long
#define MAX 1<<30
#define MIN -1<<30
#define INIT0(a) memset((a), 0, sizeof(a))
using namespace std; const double PI = 3.14159265358979323;
const double les = 0.00000005;
const long N = ;
const int S=;///判断几次 (8~12) /// (a*b)%c
LL mult_mod(LL a,LL b,LL c)
{
a%=c; b%=c;
LL ret=; LL temp=a;
while(b)
{
if(b&)
{
ret+=temp;
if(ret>c) ret-=c;
}
temp<<=;
if(temp>c) temp-=c;
b>>=1LL;
}
return ret;
} /// (a^n)%mod
LL pow_mod(LL a,LL n,LL mod)
{
LL ret=;
LL temp=a%mod;
while(n)
{
if(n&) ret=mult_mod(ret,temp,mod);
temp=mult_mod(temp,temp,mod);
n>>=1LL;
}
return ret;
} /// check a^(n-1)==1(mod n)
bool check(LL a,LL n,LL x,LL t)
{
LL ret=pow_mod(a,x,n);
LL last=ret;
for(int i=;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;
last=ret;
}
if(ret!=) return true;
return false;
} bool Miller_Rabin(LL n)
{
if(n<) return false;
if(n==) return true;
if((n&)==) return false;
LL x=n-;
LL t=;
while((x&)==) { x>>=; t++;}
srand(time(NULL)); for(int i=;i<S;i++)
{
LL a=rand()%(n-)+;
if(check(a,n,x,t)) return false;
}
return true;
} int main(){
// freopen("input1.txt", "r", stdin);
// freopen("output1.txt", "w", stdout);
int n;
cin>>n; for (int i = ; i <= ; ++i)
{
if(!Miller_Rabin(n*i+)){
cout<<i<<endl;
return ;
}
}
return ;
}
大素数判断(miller-Rabin测试)的更多相关文章
- POJ 1811 大素数判断
数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #inc ...
- 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)
关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...
- HDU 4910 Problem about GCD 找规律+大素数判断+分解因子
Problem about GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 【转】大素数判断和素因子分解【miller-rabin和Pollard_rho算法】
集训队有人提到这个算法,就学习一下,如果用到可以直接贴模板,例题:POJ 1811 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646 ...
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法) 玄学快
大数因数分解Pollard_rho 算法 复杂度o^(1/4) #include <iostream> #include <cstdio> #include <algor ...
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- 与数论的厮守01:素数的测试——Miller Rabin
看一个数是否为质数,我们通常会用那个O(√N)的算法来做,那个算法叫试除法.然而当这个数非常大的时候,这个高增长率的时间复杂度就不够这个数跑了. 为了解决这个问题,我们先来看看费马小定理:若n为素数, ...
- 与数论的爱恨情仇--01:判断大素数的Miller-Rabin
在我们需要判断一个数是否是素数的时候,最容易想到的就是那个熟悉的O(√n)的算法.那个算法非常的简单易懂,但如果我们仔细想想,当n这个数字很大的时候,这个算法其实是不够用的,时间复杂度会相对比较高. ...
- Miller Rabin算法详解
何为Miller Rabin算法 首先看一下度娘的解释(如果你懒得读直接跳过就可以反正也没啥乱用:joy:) Miller-Rabin算法是目前主流的基于概率的素数测试算法,在构建密码安全体系中占有重 ...
随机推荐
- 【hdu1255】线段树求矩形面积交
题意大概就是上图这个样子.<=100组测试数据,每组<=1000个矩形. 题解: 这个问题怎么解决..做了上一题矩形面积并应该就会了.. 对于每个节点维护3个值: cnt:该节点所代表的这 ...
- IT培训班123
最近20年,IT行业一直处于上升期,程序员的工资越来越高了,年薪几十万的程序员大有人在.根据国家统计局发布的2016年各行业平均工资报表,程序员已经是工资最高的一个群体,超过了金融行业. IT行业的火 ...
- Yii2实现读写分离(MySQL主从数据库)
读写分离(Read/Write Splitting). 1.原理: 让主数据库(master)处理事务性增.改.删操作(INSERT.UPDATE.DELETE),而从数据库(slave)处理SELE ...
- typeof的用法
typeof可以返回变量的类型,返回值为字符串,其值有 "undefined" "boolean" "string" "numbe ...
- ubuntu 提速
linux的各大发行版,都有些不必要的服务被默认开启了,针对ubuntu,我们可以采用选择性关闭的方法加速起动,提高系统性能. 这里我们安装一个软件: sudo apt-get install sys ...
- hadoop入门学习
hadoop入门学习:http://edu.csdn.net/course/detail/1397hadoop hadoop2视频:http://pan.baidu.com/s/1o6uy7Q6HDF ...
- STM32-内存管理
转载:http://www.cnblogs.com/guozhikai/p/6031904.html #ifndef __MALLOC_H #define __MALLOC_H #include &q ...
- python中的enumerate获取迭代元素的下标
以前迭代的时候,需要获取次数都是如下格式: index=1 for node in nodes: if index==3: continue print(node.text_content())ind ...
- 1438. Shopaholic
Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Lindsay is a shopaholic. Whenever th ...
- .net页面实时预览图片
<script type="text/javascript"> //获取上传图片的本地路径 function getPath(obj){ if(obj) { if(na ...