标签:des style blog http io ar color os sp
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.
input |
27 2 |
output |
YES |
input |
45 7 |
output |
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 else 28 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 }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/michaelwong/p/4133219.html