1. 程式人生 > >Goldbach`s Conjecture LightOJ - 1259 (素數打表 哥德巴赫猜想)

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

rim inf clu 就是 include str cst name long long

題意:

就是哥德巴赫猜想。。。任意一個偶數 都可以分解成兩個(就是一對啦)質數的加和

輸入一個偶數求有幾對。。

解析:

首先! 素數打表。。因為 質數 + 質數 = 偶數 所以 偶數 - 質數 = 質數 。。。 我真是蠢啊

還有 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; }

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