Codeforces Beta Round #17 A. Noldbach problem(判断素数,暴力)

A. Noldbach problem

time limit per test:2 seconds

memory limit per test:64 megabytes

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

题意:给出两个数n,k,如果在1到n中存在至少k个数满足条件,就输出YES,否则输出NO。这个条件就是这个数可表示为两个相邻素数和1的和。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<cfloat>
using namespace std;
bool is_prime(int x){
    if(x==1)
        return false;
    if(x==2||x==3)
        return true;
    if(x%6!=1&&x%6!=5)
        return false;
    int s=sqrt(x);
    for(int i=5;i<=s;i+=6)
        if(x%i==0||x%(i+2)==0)
            return false;
    return true;
}
int a[1010];
map<int,int>mp;
int main(void){
    int n,k,num=0,ans=0;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        if(is_prime(i)){
            a[num++]=i;
            mp[i]=1;
        }
    }
    for(int i=0;i<num-1;i++){
        if(mp[a[i]+a[i+1]+1]&&a[i]+a[i+1]+1<=n){
            ans++;
        }
    }
    if(ans>=k)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}
发布了90 篇原创文章 · 获赞 48 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Asher_S/article/details/82109849