pku 2262 Goldbach's Conjecture(素数筛法)

哥德巴赫猜想,不会出现"Goldbach's conjecture is wrong."。而且“choose the pair where the difference b - a is maximized.”这名话没用,不考虑就不用TLE了

#include <stdio.h>

 

#define MAXN 1000000

bool flag[MAXN];
int pn,prime[MAXN/2];
void init()
{
    for(int i=2;i<MAXN;i++)
    {
        if(!flag[i]) prime[pn++]=i;
        for(int j=0;j<pn && i*prime[j]<MAXN;j++)
        {
            flag[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
}

inline void solve(const int &n)
{
    for(int i=3;i<=n/2;i++)
    {
        if(!flag[i] && !flag[n-i])
        {
            printf("%d = %d + %d\n",n,i,n-i);
            return;
        }
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("tdata.txt","r",stdin);
    #endif
    init();
    int n;
    while(scanf("%d",&n),n)
    {
        solve(n);
    }
    return 0;
}

posted @ 2010-09-11 09:38  菜到不得鸟  阅读(116)  评论(0)    收藏  举报