Goldbach`s Conjecture LightOJ - 1259 (prime sieve)

Goldbach`s Conjecture LightOJ - 1259 (prime sieve)

Topic Link

Meaning of the questions:
known problem Goldbach conjecture
each even number greater than 2, can be divided into two prime numbers a, b and
then to subject to: a <= b;

Given n, the number of seeking a, b meet the conditions, and outputs
4 ≤ n ≤ 10 ^ 7, n is an even number

Solution:
Direct use of prime sieve: Erichsen and Euler sieve sieve can
lay a table in advance

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#define lowbit(x) x&(-x)
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000
using namespace std;
const double pi=acos(-1.0);
const int maxn=1e7;
const double eps=1e-8;
bool vis[maxn+10];
int prime[1000010];
int t,n;
int tot;
void isPrime(){
    memset(vis,true,sizeof(vis));
    vis[1]=false;
    for(int i=2;i<=maxn;i++){
        if(vis[i]==true){
            prime[tot++]=i;
        }
        for(int j=i*2;j<=maxn;j+=i){
            vis[j]=false;
        }
    }
    return ;
}
int main(){
    tot=0;
    isPrime();
    scanf("%d",&t);
    int cnt=1;
    while(t--){
        int ans=0;
        scanf("%d",&n);
        for(int i=0;prime[i]<=n/2;i++){          //循环结束条件需要注意下,开始还wa了几次
            if(vis[n-prime[i]]==true){ 
                ans++;
            }
        }
        printf("Case %d: %d\n",cnt++,ans);
    }
    return 0;
}
Published 127 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/boliu147258/article/details/104271177
Recommended