zoukankan      html  css  js  c++  java
  • [数论][Miller_Rabin] Goldbach

    Description:

    Goldbach's conjecture is one of the oldest and best-known unsolved problems in number theory and all of mathematics. It states:

    Every even integer greater than 2 can be expressed as the sum of two primes.

    The actual verification of the Goldbach conjecture shows that even numbers below at least 1e14 can be expressed as a sum of two prime numbers. 

    Many times, there are more than one way to represent even numbers as two prime numbers. 

    For example, 18=5+13=7+11, 64=3+61=5+59=11+53=17+47=23+41, etc.

    Now this problem is asking you to divide a postive even integer n (2<n<2^63) into two prime numbers.

    Although a certain scope of the problem has not been strictly proved the correctness of Goldbach's conjecture, we still hope that you can solve it. 

    If you find that an even number of Goldbach conjectures are not true, then this question will be wrong, but we would like to congratulate you on solving this math problem that has plagued humanity for hundreds of years.

    Input:

    The first line of input is a T means the number of the cases.

    Next T lines, each line is a postive even integer n (2<n<2^63).

    Output:

    The output is also T lines, each line is two number we asked for.

    T is about 100.

    本题答案不唯一,符合要求的答案均正确

    样例输入

    1
    8
    

    样例输出

    3 5

    思路:枚举n所分成的两个数,并判断它们是否同时为素数;如何判断一个较大的数(1e18左右)是否为素数:Miller_Rabin算法
    注意一些常见数据类型的取值范围:

    unsigned   int   0~4294967295   
    int   -2147483648~2147483647 
    unsigned long 0~4294967295
    long   -2147483648~2147483647

    long long的最大值:9223372036854775807
    long long的最小值:-9223372036854775808
    unsigned long long的最大值:18446744073709551615

    __int64的最大值:9223372036854775807
    __int64的最小值:-9223372036854775808
    unsigned __int64的最大值:18446744073709551615

    AC代码:
    #include <iostream>
    #include<cstdio>
    #define maxn 100010
    typedef  unsigned long long ll;//数据在运算时容易爆long long 
    using namespace std;
    
    ll qmull(ll a,ll b,ll n){//快速积
        ll ans=0;
        while(b){
            if(b&1) ans=(ans+a)%n;
            a=(a+a)%n;
            b=b>>1;
        }
        return ans;
    }
    ll qpow(ll a,ll b,ll n){//快速幂
        ll ret=1;
        ll base=a%n;
        while(b){
            if(b&1) ret=qmull(ret,base,n)%n;
            base=qmull(base,base,n)%n;
            b>>=1;
        }
        return ret;
    }
    bool Miller_Rabin(ll n){//Miller_Rabin素数检验
        ll pan[4]={2, 3, 7, 11};
        for(ll i=0; i<4; i++) if(qpow(pan[i],n-1,n)!=1) return 0;
        return 1;
    }
    
    int main()
    {
        ll t;
        scanf("%llu",&t);
        while(t--){
           ll n;
           scanf("%llu",&n);
           for(ll i=2;;i++){
             if(Miller_Rabin(i)&&Miller_Rabin(n-i)) {printf("%llu %llu
    ",i,n-i); break;}
           }
        }
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    簇索引与非簇索引在查询中的应用与分析(转载) dodo
    都是防火墙惹的祸:sqlserver连接服务器数据库提示注册失败 dodo
    C#中如何将字符串转换成流,同时如何将流转换成字符串? dodo
    dataType参数不能为空,未处理的“System.ArgumentNullException”类型的异常出现在 mscorlib.dll 中 dodo
    错误(Client does not support authentication protocol requested by server; consider upgrading MySQL client)? dodo
    php分页函数(转) dodo
    c#2.0匿名方法三(转) dodo
    ASP.NET 2.0 中改进的缓存功能(转) dodo
    解决apache服务器默认编码为西欧编码的问题 dodo
    常用的正则表达式 dodo
  • 原文地址:https://www.cnblogs.com/lllxq/p/9021115.html
Copyright © 2011-2022 走看看