LightOJ 1259 Goldbach`s Conjecture

来源:互联网 发布:如何申请淘宝网店步骤 编辑:程序博客网 时间:2024/06/10 02:06

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/F

Goldbach`s Conjecture

Description

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


由于是10^7,所以素数打表的时候用bool定义isprim数组,不然的话会超内存,之前用int定义得isprim数组,一直超内存,原来bool只占1个字节,int占4个字节,才发现bool和int还有这区别抓狂抓狂

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 10005000#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8#define met(a, b) memset (a, b, sizeof (a))bool isprime[N] = {1, 1, 0};int a[N/10], k;void prime (){    k = 0;    for (int i=2; i<N; i++)    {        if (!isprime[i])        {            a[k++] = i;            for (int j=i+i; j<N; j+=i)                isprime[j] = 1;        }    }}int main (){    int t, n, flag = 1;    scanf ("%d", &t);    prime ();    while (t--)    {        int ans = 0;        scanf ("%d", &n);        for (int i=0; i<k; i++)        {            if (a[i] > n-a[i]) break;            if (!isprime[n-a[i]])                ans++;        }        printf ("Case %d: %d\n", flag++, ans);    }    return 0;}


0 0