zoukankan      html  css  js  c++  java
  • Goldbach`s Conjecture(LightOJ

    Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】

    标签: 入门讲座题解 数论


    题目描述

    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, ...
    

    题意

    哥德巴赫猜想:任意大于等于2的偶数,可以拆分成两个质数加和的形式。(该猜想真伪至今未得完整证明。)

    给你一个(n),问(n)按照哥德巴赫猜想有几种拆分方法?((n leq 1e7))

    解析

    哥德巴赫猜想虽然无法证明,但在较小的数据范围内,我们是可以通过计算机求得解的。

    我们先通过线性筛找到(1e7)内的全体质数,不超过(67e4)个.这样我们对于每个(n),只需要循环一遍质数,看看(n)与这个质数的差是否还是质数就可以了。这个差如果是质数,那么就多一种方案.
    因为我们存有(vis[1 cdots n])数组,所以可以(O(1))的判断所求差是不是一个质数.

    注意:本题可以使用欧拉筛也可以使用埃氏筛(我看vjudge上的提交代码有埃氏筛的)。

    考察内容:素数筛法

    通过代码

    /*
    Problem
        LightOJ - 1259
    Status
    	Accepted
    Time
    	236ms
    Memory
    	14468kB
    Length
    	798
    Lang
    	C++
    Submitted
    	2019-11-24 22:23:13
    Shared
    	
    RemoteRunId
    	1640384*/
    
    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 1e7 + 50;
    
    int prime[670000], cnt = 0;    //注意这题有内存限制,prime数组开到1e7会RE(MLE).我提前打表看了一下,1e7以内的质数不超过67e4个.
    bool vis[MAXN];
    
    void init()     //欧拉筛。线性时间复杂度内筛出质数来.prime数组存质数,vis[i]数组判断i是否为质数.
    {
        vis[1] = 1;
    
        for(int i = 2; i <= int(1e7 + 5); i ++){
    
            if(!vis[i])
                prime[++ cnt] = i;
    
            for(int j = 1; j <= cnt && i * prime[j] <= int(1e7 + 5); j ++){
                vis[i * prime[j]] = 1;
                if(i % prime[j] == 0)
                    break;
            }
        }
    
        return ;
    }
    
    int main()
    {
        init();  
    
        int T, n, times = 0;
        
        scanf("%d", &T);
        
        while(T --){
            scanf("%d", &n);
    
            int ans = 0;
            for(int i = 1; i <= cnt && prime[i] <= n / 2; i ++){    //只需循环到n/2即可.在(n/2, n]区间中出现的序偶<a, b>,在[2, n/2]区间也一定出现过.
    
                if(!vis[n - prime[i]]){
                    ans ++;
               }
            }
            printf("Case %d: %d
    ", ++ times, ans);
        }
        return 0;
    
    }
    
    
  • 相关阅读:
    Hive之序列化与反序列化(SerDe)
    Hive从入门到精通
    HIVE从路人到入门
    Intel IDEA 2018破解(亲测成功)
    在小红家里面,有n组开关,触摸每个开关,可以使得一组灯泡点亮。
    一个n*n 的方格,要从左上角走到右下角,一次只能往右或往下走一步,求算法得出所有走动的方法数。
    Ionic3多个自定义过滤器--管道(pipe)
    ionic3 添加多个自定义组件
    ionic3 slides轮播图手动滑动后无法自动播放问题
    Vue2.2版本学习小结
  • 原文地址:https://www.cnblogs.com/satchelpp/p/11924964.html
Copyright © 2011-2022 走看看