poj 2262 Goldbach's Conjecture——筛质数(水!)

题目:http://poj.org/problem?id=2262

大水题的筛质数。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6;
int n,pri[N+5],cnt;
bool vis[N+5];
void init()
{
  for(int i=2;i<=N;i++)
    {
      if(!vis[i])pri[++cnt]=i;
      for(int j=1;j<=cnt&&(long long)i*pri[j]<=N;j++)
    {
      vis[i*pri[j]]=1;
      if(i%pri[j]==0)break;
    }
    }
}
int main()
{
  init();
  while(1)
    {
      scanf("%d",&n);if(!n)return 0;
      bool flag=0;
      for(int i=2;i<=cnt&&n>pri[i];i++)
        if(!vis[n-pri[i]])
          {
            printf("%d = %d + %d\n",n,pri[i],n-pri[i]);
            flag=1;break;
          }
      if(!flag)printf("Goldbach's conjecture is wrong.\n");
    }
}

 

posted on 2018-07-07 17:22  Narh  阅读(110)  评论(0编辑  收藏  举报

导航