题目: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. iOS开发者两分钟学会用GitHub在Mac上托管代码的两种方法

        原文发布者:http://blog.csdn.net/duxinfeng2010 在Mac上使用Xcode进行iOS-Apple苹果iPhone手机开发过程中少不了使用GitHub在Mac上托 ...

  2. javascript中break和continue

    1.break break语句会立即退出循环,强制执行循环后面的语句 var num = 0; for(var i=1;i<10;i++){ if(i%5 == 0){ break; } num ...

  3. 初识费用流 模板(spfa+slf优化) 餐巾计划问题

    今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...

  4. perl6中的q/qq/qx/qqx

    q不内插 qq内插 qx不内插 qqx内插

  5. Perl6 Bailador框架(5):利用正则匹配路径

    use v6; use Bailador; =begin pod 我们在路径设置上, 可以利正则表达式捕获的字符串作为子例程参数 =end pod get '/perl6/(.+)' => su ...

  6. url编码模块

    use LWP::SImple; use URI::Escape; encoded_string = uri_escape(raw_string); get(encoded_string);

  7. 【转】cve-2013-2094 perf_event_open 漏洞分析

    cve-2013-2094是于2013年4月前后发现的linux kernel本地漏洞,该漏洞影响3.8.9之前开启了PERF_EVENT的linux系统.利用该漏洞,通过perf_event_ope ...

  8. python基础===python os.path模块

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  9. 【Educational Codeforces Round20】

    这场edu有点简单…… 所以题目可能也有点奇奇怪怪的. A.随意构造一下,可以发现只有当填满都不行时才可能无解. #include<bits/stdc++.h> using namespa ...

  10. #error This file was generated by a newer version of protoc

    pattern@pattern89:/raid0/workspace/houjun/caffe-ssd$ sudo make all -j8PROTOC src/caffe/proto/caffe.p ...