poj2262 Goldbach's Conjecture

poj2262 Goldbach's Conjecture

欧拉筛把素数筛出来,再枚举一下。

#include<iostream>
#include<cstdio>
#include<cstring>
#define re register
using namespace std;
#define N 1000001
int n,v[N],pri[N],cct; bool is[N];
int main(){
    for(int i=2;i<N;++i){
        if(!v[i]) is[v[i]=pri[++cct]=i]=1;
        for(int j=1;j<=cct;++j){
            if(pri[j]>i||pri[j]*i>=N) break;
            v[pri[j]*i]=pri[j];
        }
    }
    while(scanf("%d",&n)){
        if(!n) return 0;
        bool flag=1;
        for(int i=2;i<=cct&&flag;++i)
            if(is[i]&&is[n-i])
                printf("%d = %d + %d\n",n,i,n-i),flag=0;
        if(flag) puts("Goldbach's conjecture is wrong.");
    }return 0;
}
View Code

 

posted @ 2018-10-31 15:44  kafuuchino  阅读(126)  评论(0编辑  收藏  举报