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/
  • 相关阅读:
    UVa 10118 记忆化搜索 Free Candies
    CodeForces 568B DP Symmetric and Transitive
    UVa 11695 树的直径 Flight Planning
    UVa 10934 DP Dropping water balloons
    CodeForces 543D 树形DP Road Improvement
    CodeForces 570E DP Pig and Palindromes
    HDU 5396 区间DP 数学 Expression
    HDU 5402 模拟 构造 Travelling Salesman Problem
    HDU 5399 数学 Too Simple
    CodeForces 567F DP Mausoleum
  • 原文地址:https://www.cnblogs.com/lllxq/p/9021115.html
Copyright © 2011-2022 走看看