lightoj 1259

1259 - Goldbach`s Conjecture
Time Limit: 2 second(s) Memory Limit: 32 MB

Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:

Every even integer, greater than 2, can be expressed as the sum of two primes [1].

Now your task is to check whether this conjecture holds for integers up to 107.

Input

Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).

Output

For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where

1)      Both a and b are prime

2)      a + b = n

3)      a ≤ b

Sample Input

Output for Sample Input

2

6

4

Case 1: 1

Case 2: 1

Note

1.      An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...

题意是问你对于一个n有几对a和b均为素数,并且a+b=n,a<=b
直接打表判断就好了

#include <stdio.h>
const int MAXN = 1e7 + 10;
int prime[700005];
bool isprime[MAXN];
void init() {
    int p = 0;
    for (int i = 0; i < MAXN; i++) isprime[i] = true;
    isprime[0] = isprime[1] = false;
    for (int i = 2; i < MAXN; i++) {
        if (isprime[i]) {
            prime[p++] = i;
            for (int j = 2*i; j < MAXN; j += i) isprime[j] = false;
        }
    }
}
int main(int argc, char const *argv[])
{
    int T;
    init();
    scanf("%d", &T);
    int Kcase = 0;
    while (T--) {
        int N;
        scanf("%d", &N);
        int ans = 0;
        for (int i = 0; 2*prime[i] <= N; i++) {
            if (isprime[N - prime[i]]) ans++;
        }
        printf("%s%d: %d
", "Case ", ++Kcase, ans);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/cniwoq/p/6770738.html