17A. Noldbach problem
2017-03-04 20:47
375 查看
A. Noldbach problem
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
Note
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.
给出n,问在[0-n] 区间内是否能找到K个数,要求这k个数都是素数且每个数都能写成三个数的的和,且这三个数中,一个是1,另外两个数是相邻的素数
素数打表+记录
#include<cstdio>
const int maxn=10000;
int prime[maxn],num[maxn*2],result[maxn*2];
void work()
{
int a=0,b=2;
for(int i=2;i<maxn;++i)
{
if(prime[i]==0)
{
if(i>2)
{
a=b;b=i;
num[a+b+1]=1;
}
for(int j=2*i;j<maxn;j+=i)
{
prime[j]=1;
}
}
}
for(int i=2;i<maxn;++i)
{
if(prime[i]==0&&num[i]==1)
{
result[i]=1;
}
result[i]+=result[i-1];
}
}
int main()
{
work();
int n,k;
while(~scanf("%d%d",&n,&k))
{
printf("%s\n",result
>=k?"YES":"NO");
}
return 0;
}
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
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.
Examples
input
27 2
output
YES
input
45 7
output
NO
Note
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.
给出n,问在[0-n] 区间内是否能找到K个数,要求这k个数都是素数且每个数都能写成三个数的的和,且这三个数中,一个是1,另外两个数是相邻的素数
素数打表+记录
#include<cstdio>
const int maxn=10000;
int prime[maxn],num[maxn*2],result[maxn*2];
void work()
{
int a=0,b=2;
for(int i=2;i<maxn;++i)
{
if(prime[i]==0)
{
if(i>2)
{
a=b;b=i;
num[a+b+1]=1;
}
for(int j=2*i;j<maxn;j+=i)
{
prime[j]=1;
}
}
}
for(int i=2;i<maxn;++i)
{
if(prime[i]==0&&num[i]==1)
{
result[i]=1;
}
result[i]+=result[i-1];
}
}
int main()
{
work();
int n,k;
while(~scanf("%d%d",&n,&k))
{
printf("%s\n",result
>=k?"YES":"NO");
}
return 0;
}
相关文章推荐
- Codeforces 17A Noldbach problem(数学)
- codeforces 17A Noldbach problem
- 17A Noldbach problem
- CodeForces-17A-Noldbach problem
- D - Noldbach problem CodeForces - 17A
- A. Noldbach problem
- cf17A Noldbach problem (素数打表)
- 【水遍历\打表】#17 A. Noldbach problem
- Noldbach problem
- Codeforces Beta Round #17 A - Noldbach problem 暴力
- CodeForces 17 A.Noldbach problem(水~)
- 【codeforces 749A】Bachgold Problem
- 【codeforces 749A】Bachgold Problem
- 【codeforces 749A】Bachgold Problem
- Codeforces 749A-Bachgold Problem
- 【codeforces 749A】Bachgold Problem
- A - Bachgold Problem vj
- 749 A. Bachgold Problem
- cf 749 A. Bachgold Problem
- Bachgold Problem CodeForces - 749A