首页 > 代码库 > Noldbach problem

Noldbach problem

Description
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.

Sample test(s)
input
27 2
output
YES
 
 
 
 
 
 
input
45 7
output
NO
 
 
 
 
 
 
题解:
  如果一个素数可以用三个素数之和表示,其中一个为1,另外两个为相邻的素数,则计数一次。
  从2到n,存在上述的素数不少于k个,输出YES,否则输出NO。
代码:
 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4  5 int isPrime(int n); //判断n是否是素数 6 void initPrime(int n);  //将n以内的素数存到数组内 此处用1000即可 7  8 using namespace std; 9 int p[200]; //存储2~1000之间的素数10 int main()11 {12     int n, k, i, j, counter=0;13     cin >> n >> k;14     initPrime(1000);15     for(i=2; i<=n; i++) {16         if(isPrime(i)==0)17             continue;18         for(j=0; p[j]<i; j++) {19             if(p[j]+p[j+1]+1 == i){20                 counter++;21                 break;22             }23         }24     }25     if(counter < k)26         cout << "NO" <<endl;27     else28         cout << "YES" <<endl;29     return 0;30 }31 32 void initPrime(int n) {33     int i, j = 0;34     for(i=2; i<n; i++) {35         if(isPrime(i)){36             p[j++] = i;37         }38     }39 }40 41 int isPrime(int n) {42     int i;43     for(i=2; i<=(int)sqrt(n); i++) {44         if(n%i == 0) {45             return 0;46         }47     }48     return 1;49 }

 

Problem -17A - Codeforces

转载请注明出处:http://www.cnblogs.com/michaelwong/p/4133219.html

Noldbach problem