【codeforces 755A】PolandBall and Hypothesis

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
Note
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

For the first sample testcase, 3·1 + 1 = 4. We can output 1.

In the second sample testcase, 4·1 + 1 = 5. We cannot output 1 because 5 is prime. However, m = 2 is okay since 4·2 + 1 = 9, which is not a prime number.

【题目链接】:http://codeforces.com/contest/755/problem/A

【题解】

枚举m从1..1000就好;
(special way 注意到当m=n+2的时候,n*m+1=(n+1)^2,而当m=n-2的时候,n*m+1=(n-1)^2,所以对n分类讨论一下就可以了.)
代码给的是枚举的.

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int MAXN = 110;

int n;

bool is(int k)
{
        int len = sqrt(k);
        rep1(j,2,len)
            if (k%j==0)
            {
                return false;
            }
        return true;
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    rei(n);
    rep1(m,1,1000)
    {
        int k = n*m+1;
        //cout << k <<endl;
        if (!is(k))
        {
            printf("%d
",m);
            return 0;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626717.html