A. Noldbach problem
来源:互联网 发布:淘宝联盟如何推广赚钱 编辑:程序博客网 时间:2024/06/01 10:07
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.
27 2
YES
45 7
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.
筛法+暴力
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int used[maxn];
void set1(){
memset(used,1,sizeof used);
for(int i=2;i<=1000;i++){
if(used[i]){
for(int j=i+i;j<=1000;j+=i)
used[j]=0;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
set1();
int n,p;
while(cin>>n>>p){
int num=0;
int i,j;
for(int k=2;k<=n;k++){
if(used[k])
for( i=2;i<=k;i++)
if(used[i]){
for( j=i+1;j<=k;j++){
if(used[j]){
if(i+j+1==k)num++;
break;
}
}
}
}
//cout<<num<<endl;
if(num<p)cout<<"NO";
else cout<<"YES";
}
return0;
}
- A. Noldbach problem
- 17A. Noldbach problem
- codeforces 17A Noldbach problem
- CF 17A Noldbach problem
- cf 17a Noldbach problem
- CodeForces-17A-Noldbach problem
- Codeforces 17A Noldbach problem(数学)
- D - Noldbach problem CodeForces - 17A
- CodeForces 17 A.Noldbach problem(水~)
- 【水遍历\打表】#17 A. Noldbach problem
- cf17A Noldbach problem (素数打表)
- Problem A
- Problem A
- Problem A
- Problem A
- Problem A
- Problem A
- Problem A
- A. Numbers
- A. Letter
- A. Cottage Village
- Mongoose使用操作
- A. Flag
- A. Noldbach problem
- A. Triangle
- A. World Football Cup
- A. BerOS file system
- A. Jabber ID
- A. Second Order Statistics
- A. You\'re Given a String...
- A. Ring road
- A. IQ test