[数论][Miller_Rabin] Goldbach

Posted lz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[数论][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\n",i,n-i); break;}
       }
    }
    return 0;
}

以上是关于[数论][Miller_Rabin] Goldbach的主要内容,如果未能解决你的问题,请参考以下文章

图个安心的数论类简单模板

Miller-Rabin与二次探测

hdu2138 Miller_Rabin

Miller_Rabin

Miller_Rabin

Miller_Rabin素数测试