题目:PolandBall and Hypothesis

A. PolandBall and Hypothesis
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The only number in the input is n (1 ≤ n ≤ 1000) — number from the PolandBall's hypothesis.

Output

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.

Examples
input
3
output
1
input
4
output
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测试)的更多相关文章

  1. POJ 1811 大素数判断

    数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #inc ...

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

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

  3. HDU 4910 Problem about GCD 找规律+大素数判断+分解因子

    Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. 【转】大素数判断和素因子分解【miller-rabin和Pollard_rho算法】

    集训队有人提到这个算法,就学习一下,如果用到可以直接贴模板,例题:POJ 1811 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646 ...

  5. 大素数判断和素因子分解(miller-rabin,Pollard_rho算法) 玄学快

    大数因数分解Pollard_rho 算法 复杂度o^(1/4) #include <iostream> #include <cstdio> #include <algor ...

  6. 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

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

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

  8. 与数论的爱恨情仇--01:判断大素数的Miller-Rabin

    在我们需要判断一个数是否是素数的时候,最容易想到的就是那个熟悉的O(√n)的算法.那个算法非常的简单易懂,但如果我们仔细想想,当n这个数字很大的时候,这个算法其实是不够用的,时间复杂度会相对比较高. ...

  9. Miller Rabin算法详解

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

随机推荐

  1. bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛——差分

    Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. E ...

  2. bzoj 1965 数学

    首先我们可以发现每张牌的对应关系,假设序号为x的牌,经过一次洗牌后的位置为: 2*x     x<=n/2 2*(x-n/2)-1 x>n/2 那么我们可以将下面的式子化简,变成2*x-n ...

  3. Linux内核模块编程可以使用的内核组件

    2.2.2 在阅读<深入Linux内核架构与底层原理> 作者:刘京洋 韩方,发现一些错误,有些自己的理解,特以此记录 1.工作队列(workqueue) 队列是一种可以先进先出的数据结构, ...

  4. xtrabackup 安装、备份和恢复

    xtrabackup 版本对应: 2.4 专针对 5.7 开发的,兼容 5.6, 5.5 2.3 针对 5.6 开发的,兼容5.5 2.2 针对5.5 开发的 安装包下载: wget https:// ...

  5. linux和性能相关的命令及系统性能诊断

    常用的和性能有关的命令 Iostat/vmstat/top/mpstat/time/strace/ipcs/ipcrm/ifconfig/tethereal/netstat/free/uptime 关 ...

  6. [ python ] 类中的一些特殊方法

    item系列 __getitem__(self, item) 对象通过 object[key] 触发 __setitem__(self, key, value) 对象通过 object[key] = ...

  7. JS实现全选与取消 Jquery判断checkbox是否被选中

    1.JS实现checkbox全选与取消 <body> <input type="checkbox" name="select_all"/> ...

  8. thinkphp模板常用的方法

    thinkphp模板我是看了3.2的文档,对里面的东西过了一遍,然后在写到需要用到模板的东西的时候就有印象,有的能直接回顾,但是有的就可能只知道有这个东西,但是不知道怎么用,所以就重新查手册,这个的话 ...

  9. IE11中实现颜色渐变

    background: -ms-linear-gradient(left,#daa23e,#ad7f27); 下面是css3中颜色渐变对各个浏览器的写法:background: -webkit-lin ...

  10. Redis -- 过期时间 和 缓存 例子

    1.设置 key的生存时间,过期自动删除 exprire key  seconds    设置过期时间 秒数 ttl key   查询剩余时间 如果 设置了过期时间.对key进行 set 操作,会清除 ...