数学——Noldbach problem

Noldbach problem

题面翻译

若一个素数可以用比它小的和它相邻的两个素数的和+1表示,那么称这个素数为"好素数"。
给定两个正整数n,k,若从2到n的好素数个数大于等于k,那么输出"YES",否则输出"NO"。

2<=n<=1000
0<=k<=1000

Translated by 稀神探女

题目描述

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.

输入格式

The first line of the input contains two integers $ n $ ( $ 2<=n<=1000 $ ) and $ k $ ( $ 0<=k<=1000 $ ).

输出格式

Output YES if at least $ k $ prime numbers from $ 2 $ to $ n $ inclusively can be expressed as it was described above. Otherwise output NO.

样例 #1

样例输入 #1

27 2

样例输出 #1

YES

样例 #2

样例输入 #2

45 7

样例输出 #2

NO

提示

In the first sample the answer is YES since at least two numbers can be expressed as it was described (for example, 13 and 19). In the second sample the answer is NO since it is impossible to express 7 prime numbers from 2 to 45 in the desired form.

思路

这道题个人感觉翻译讲的不清楚,看了英文才知道意思:讲的就是找:假设我们现在遍历的是 i i i这个的质数,我们要找它前面的(也就是不能跟 i i i这个质数相邻)的相邻的质数和+1=改质数。

然后我们再用质数筛就好了。

代码

//质数筛

#include<iostream>
#include<cstring>

using namespace std;

const int N = 1e5+10;

int primes[N],cnt;
bool st[N];
int n,k;
int ans;

void init(int x){
    st[1]=true;
    for(int i=2;i<=x;i++){
        if(!st[i])primes[cnt++]=i;
        for(int j=0;i*primes[j]<=x;j++){
            st[i*primes[j]]=true;
            if(i%primes[j]==0)break;
        }
    }
}

int main(){
    
    cin>>n>>k;
    init(n);
    
    for(int i=3;i<=cnt;i++){
        for(int j=1;j<=i-2;j++){
            if(primes[j]+primes[j+1]+1==primes[i]){
                ans++;
                // cout<<primes[i]<<endl;
            }
        }
    }
    
    
    if(ans>=k){
        puts("YES");
    }else{
        puts("NO");
    }
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值