D - Noldbach problem CodeForces - 17A

Nick is interested in prime numbers. Once he read about Goldbach problem. It states that every even integer greater than 2 can be expressed as the sum of two primes. That got Nick’s attention and he decided to invent a problem of his own and call it Noldbach problem. Since Nick is interested only in prime numbers, Noldbach problem states that at least k prime numbers from 2 to n inclusively can be expressed as the sum of three integer numbers: two neighboring prime numbers and 1. For example, 19 = 7 + 11 + 1, or 13 = 5 + 7 + 1.
Two prime numbers are called neighboring if there are no other prime numbers between them.
You are to help Nick, and find out if he is right or wrong.
Input
The first line of the input contains two integers n (2 ≤ n ≤ 1000) and k (0 ≤ k ≤ 1000).
Output
Output YES if at least k prime numbers from 2 to n inclusively can be expressed as it was described above. Otherwise output NO.
Example
Input
27 2
Output
YES
Input
45 7
Output
NO
题意:判断2到n个数内有多少个数等于两个相邻素数和数字一之和。如果数目大于k,则输出YES,否则输出NO
思路:由于数据范围比较小,所以利用筛选素数暴力求出1000+10内的全部素数。然后从10到n开始枚举是否符合,可以从从把这个数减1再除以二(此时这个数也可能是素数)然后再从两侧找到最近的素数,如果两个的和加一等于则符合。
代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX 1000+10
bool prime[MAX];
bool judge(int x)//判断这个数是否符合。 
{
    x -= 1;
    int r = x/2;
    int i,j;
    if(prime[r] == 1)
        i = r;
    else
        i = r-1;
    j = r + 1;
    while(prime[i] == 0)
        i--;
    while(prime[j] == 0)
        j++;
    if((i+j) == x)
        return 1;
    else
        return 0;
}
int main(void)
{
    int i,j;
    for(i=1;i<=1000;i++)//开始筛选素数 
        prime[i] = 1;
    prime[1] = 0;
    for(i=2;i<=100;i++)
    {
        if(prime[i] == 1)
        {
            for(j=i+1;j<=1000;j++)
            {
                if(j % i == 0 && prime[j] == 1)
                    prime[j] = 0;
            }
        }
    }
    int n,k;
    scanf("%d %d",&n,&k);
    int sum = 0;
    for(i=10;i<=n;i++)
    {
        if((judge(i)) == 1 && prime[i] == 1)
            sum++;
    }
    if(sum >= k)
        printf("YES");
    else
        printf("NO");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值