• CodeForces 755A-PolandBall and Hypothesis(思维题)


    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 ≤ 1e3. 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.

    波兰球是一个年轻,聪明的球。他对质数感兴趣。他提出了以下假设:“存在这样一个正整数n,对于每个正整数m数n·m + 1是质数”。

    不幸的是,波兰球还没有经验,也不知道他的假设是错误的。你能证明错吗?编写一个程序,查找任何n的反例。

    输入值
    输入中的唯一数字是n(1≤n≤1000)-来自PolandBall假设的数字。

    输出量
    输出n·m + 1不是素数的m。如果输出任何合适的m使得1≤m≤1e3,则您的答案将被认为是正确的,可以确保答案存在。

    例子
    输入值
    3
    输出量
    1个
    输入值
    4
    输出量
    2
    注意
    质数(或素数)是大于1的自然数,除1及其本身外,没有除数。

    对于第一个样本测试用例,3·1 + 1 = 4.我们可以输出1。

    在第二个示例测试用例中,4·1 + 1 = 5.我们不能输出1,因为5是素数。但是,因为4·2 + 1 = 9(不是素数),所以m = 2是可以的。

    这道题是CF上的题,输入一个n,如果n x 一个1-1000范围内的m+1不是素数,则输出m,这道题有两种解法。
    思路一:最直白的是暴力,因为范围是1000,直接枚举1-1000的m,判断一下n*m+1是不是素数即可,暴力做法:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    int main()
    {
    	bool check(int);
    	int n;
    	cin>>n;
    	for(int i=1;i<=1000;i++)
    	  if(check(n*i+1))//判断一下是不是素数
    	  {
    	  	cout<<i<<endl;
    	  	break;//如果不是则退出循环
    	  }
    	return 0;
    }
    bool check(int s)
    {
    	if(s==1)
    	  return true;
    	for(int i=2;i<=(int)sqrt(s);i++)
    	  if(s%i==0)
    	    return true;
    	return false;
    }
    

    思路二(思维):通过完全平方公式解题,因为(n+1)方 和(n-1)方一定不是素数,我们以(n+1)为例,n+1的平方可以分解为n+2n+1=n(n+2)+1,我们不难发现,n+2就是我们要求的m,因为数据范围是1-1000,如果n+2比1000大的话,我们令m=n-2即可。AC代码

    #include <cstdio>
    #include <iostream>
    using namespace std;
    const int _max=1e3;
    int main()
    {
    	int n,m;
    	cin>>n;
    	if(n+2>_max)
    	  m=n-2;
    	else
    	  m=n+2;
    	cout<<m<<endl;
    	return 0;
    }
    
  • 相关阅读:
    HTML中的文本标签
    Java 数组的创建
    JavaScript实现LUHN算法验证银行卡号有效性
    JavaScript实现HTML页面集成QQ空间分享功能
    JavaScript中的三种弹出框的区别与使用
    Maven 项目中的 pom.xml 文件内容说明
    FTPClient 中 FTPClient.changeWorkingDirectory(filePath) 代码一直返回 false
    Eclipse 中 Debug 时鼠标悬停无法查看变量值
    Innodb ,MyISAM
    tomcat jetty
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294323.html
Copyright © 2020-2023  润新知