Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)

题意:

就是哥德巴赫猜想。。。任意一个偶数 都可以分解成两个(就是一对啦)质数的加和

输入一个偶数求有几对。。

解析:

首先! 素数打表。。因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质数 。。。 我真是蠢啊

还有  vis要用bool类型的!!!!  int会直接爆

代码如下:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define maxn 10000000
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int LL_INF = 0x7fffffffffffffff,INF = 0x3f3f3f3f;
LL primes[700000];
bool vis[maxn];
int cnt = 0;
void init()
{
    mem(vis,0);
    primes[1] = 1; primes[0] = 1;
    for(int i=2; i<maxn; i++)
        if(!vis[i])
        {
            primes[cnt++] = i;
            for(LL j=(LL)i*i; j<maxn; j+=i)
                vis[j] = 1;
        }
}


int main()
{
    int T;
    init();
    int res = 0;
    cin>> T;
    while(T--)
    {
        int ans = 0;
        int n;
        cin>> n;
        for(int i=0; i<cnt && primes[i] <= n/2; i++)
        {
            if(!vis[n-primes[i]])
                ans++;
        }
        printf("Case %d: %d\n",++res,ans);

    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/9184259.html
今日推荐