Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:
Every even integer, greater than 2, can be expressed as the sum of two primes [1].
Now your task is to check whether this conjecture holds for integers up to 107.
Input
Input starts with an integer T (≤ 300), denoting the number of test cases.
Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).
Output
For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where
1) Both a and b are prime
2) a + b = n
3) a ≤ b
Sample Input
2
6
4
Sample Output
Case 1: 1
Case 2: 1
Note
1. An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...
题解:
大力筛出1e7内的素数,然后暴力查找满足条件的数对,然后输出
代码:
//Full of love and hope for life
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/
//https://www.cnblogs.com/zzrturist/ //博客园
//https://blog.csdn.net/qq_44134712 //csdn
using namespace std;
typedef long long ll;
const ll maxn=1e7+10;
vector<int>n;
bool prime[maxn];
void inti(){
prime[1]=true;
for(ll i=2;i<maxn;i++){
if(prime[i]==false){
n.push_back(i);
for(ll j=i+i;j<maxn;j+=i){
prime[j]=true;
}
}
}
}
int main(){
ll a,b,k=0,ans;
inti();
cin >> a;
while(a--){
ans=0;
k++;
cin >> b;
for(ll i=0;i<n.size();i++){
if(n[i]>b-n[i]){
break;
}
if(prime[b-n[i]]==false){
ans++;
}
}
cout << "Case " << k << ": " << ans << endl;
}
return 0;
}