F - Goldbach`s Conjecture kuangbin 基础数论

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

Hint

  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, ...
  2. #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<iomanip>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define MAXN 10000001
    #define L 31
    #define INF 1000000009
    #define eps 0.00000001
    /*
    打表 把所有素数存到一个vector中
    然后用一个map保存所有和出现的次数
    然后直接找就可以
    */
    bool notprime[MAXN];
    vector<int> prime;
    
    void Init()
    {
        memset(notprime, false, sizeof(notprime));
        notprime[1] = true;
        for (int i = 2; i < MAXN; i++)
        {
            if (!notprime[i])
            {
                prime.push_back(i);
                for (int j = i + i; j < MAXN; j += i)
                    notprime[j] = true;
            }
        }
    }
    int main()
    {
        Init();
        int T,n;
        cin >> T;
        for(int cas=1;cas<=T;cas++)
        {
            cin >> n;
            vector<int>::iterator p = lower_bound(prime.begin(), prime.end(), n/2);
            //cout << *p << endl;
            int cnt = 0;
            for (vector<int>::iterator it = prime.begin(); it <= p && *it<=n/2; it++)
            {
                if (!notprime[n - *it])
                {
                    //cout << *it << ‘ ‘ << n - *it << endl;
                    cnt++;
                }
            }
            printf("Case %d: %d\n", cas, cnt);
        }
        return 0;
    }
时间: 2024-11-10 07:34:59

F - Goldbach`s Conjecture kuangbin 基础数论的相关文章

F - Goldbach`s Conjecture(哥德巴赫猜想)

题目大致的意思是输出一个偶数总共有多少个素数对构成哥德巴赫猜想(PS:陷阱好多,稍一不慎就超内存了) 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 const long long maxn=1e7+5; 8 9 bool ip[maxn];//使用long long会超内存 10 long long T,n,p[1000005];//

F - Goldbach`s Conjecture

题目链接:https://vjudge.net/problem/LightOJ-1259#author=yyb 题目大意: T组询问,每组询问是一个偶数n,验证哥德巴赫猜想,回答n=a+b,且a,b(a<=b)是质数的方案个数. 解题思路: 欧拉筛,遍历到 $2^{*}$prime$[j]<=n$ ,其中令$a=prime[j],b=n-prime[j]$,判断b是否为质数即可. 代码: 1 #include <bits/stdc++.h> 2 using namespace st

[kuangbin 基础dp][POJ 1015] Jury Compromise(dp)

[kuangbin 基础dp][POJ 1015] Jury Compromise 题目 In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is do

poj 2262 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39353   Accepted: 15077 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

Goldbach&#39;s Conjecture(哥德巴赫猜想)

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5277    Accepted Submission(s): 2022点我 Problem Description Goldbach's Conjecture: For any even number n greater than or equa

POJ 2262 Goldbach&#39;s Conjecture (素数判断)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37693   Accepted: 14484 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

题目1440:Goldbach&#39;s Conjecture

题目1440:Goldbach's Conjecture 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not been prove

POJ 2262 Goldbach&#39;s Conjecture (求解素数的一般筛和线性筛)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40944   Accepted: 15664 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;s Conjecture 数学常识 难度:0

题目链接:http://poj.org/problem?id=2262 哥德巴赫猜想肯定是正确的 思路: 筛出n范围内的所有奇质数,对每组数据试过一遍即可, 为满足b-a取最大,a取最小 时空复杂度分析: 在1e6内约有8e4个奇质数,因为a <= b,时间复杂度在T*4e4+1e6等级.一般T为1e3,足以承受 空间复杂度为1e6,足以承受 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm&g