POJ 2262 Goldbach's Conjecture (prime correlation), pojconjecture
POJ 2262 Goldbach's Conjecture (prime correlation)
Http://poj.org/problem? Id = 2262
Question:
To give you an even number in the range of [6,], you need to represent it in the form of two prime numbers plus each other. If multiple groups of solutions exist, output the solution with the greatest difference between two prime numbers.
Analysis:
First, we use the prime number embedding method to obtain all prime numbers within W.
The 'distinct' method is used to find the prime number:
Http://blog.csdn.net/u013480600/article/details/41120083
For a given number X, if there is a prime number a + prime number B = X, and the gap between a and B is the largest. Then we only need to enumerate prime number a from small to large, and then use X-a to be B. Then judge whether B is a prime number. (Think about it?)
AC code:
# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int maxn = 1000000; // bool not_prime [maxn + 5] by prime number embedding; // not_prime [I] = true table I is not a prime number int prime [maxn + 5]; int get_prime () {memset (prime, 0, sizeof (prime )); // Note: When not_prime [I] = false, I is a prime memset (not_prime, 0, sizeof (not_prime); for (int I = 2; I <= maxn; I ++) {if (! Not_prime [I]) prime [++ prime [0] = I; for (int j = 1; j <= prime [0] & prime [j] <= maxn/I; j ++) {not_prime [I * prime [j] = true; if (I % prime [j] = 0) break;} return prime [0];} int main () {// generate all prime numbers within W get_prime (); int x; while (scanf ("% d", & x) = 1 & x) {int a, B; // x is split into two numbers: a and B. bool OK = false; for (int I = 1; I <= prime [0] & prime [I] <= x/2; I ++) {a = prime [I]; B = x-a; if (! Not_prime [B]) // if B is a prime number {OK = true; break ;}} if (OK) printf ("% d = % d + % d \ n ", x, a, B); else printf ("Goldbach's conjecture is wrong. \ n ") ;}return 0 ;}