UVa10311 - Goldbach and Euler

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int N = 100000000 + 1;

bool vis[N];
vector<int> vPrime;

void sieve();

int main()
{
    int n;
    bool flag;

#ifndef ONLINE_JUDGE
    freopen("uva_in.txt", "r", stdin);   
#endif

    sieve();
    while (scanf("%d", &n) == 1) {
        if ((n & 1) == 0) {
            //printf("even\n");
            flag = true;
            for (int i = n / 2; i >= 1; i--) {
                if (vis[i] && vis[n - i] && i != n - i) {
                    printf("%d is the sum of %d and %d.\n", n, i, n - i);
                    flag = false;
                    break;
                }
            }
            if (flag) {
                printf("%d is not the sum of two primes!\n", n);
            }    
        } else {
            //printf("odd\n");
            if (n > 2 && vis[n - 2]) {
                printf("%d is the sum of %d and %d.\n", n, 2, n - 2);
            } else {
                printf("%d is not the sum of two primes!\n", n);
            }
        }
    }


    return 0;
}

void sieve()
{
    memset(vis, true, sizeof(vis));
    vis[0] = false;
	vis[1] = false;


    for (int i = 2; i < 10000; i++) {
        if (vis[i]) {
            for (int j = i * i; j < N; j += i) {
                vis[j] = false;
            }
        }
    }

    /*
    for (int i = 1; i < N; i++) {
        if (vis[i]) {
            vPrime.push_back(i);
        }
    }
    */
}

你可能感兴趣的:(UVa10311 - Goldbach and Euler)