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

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;
typedef unsigned long long LL;
const int S = 8;

LL mult_mod(LL a, LL b, LL c) {
    a %= c;
    b %= c;
    LL ret = 0;
    LL tmp = a;
    while(b) {
        if(b & 1) {
            ret += tmp;
            if(ret > c)ret -= c;
        }
        tmp <<= 1;
        if(tmp > c)tmp -= c;
        b >>= 1;
    }
    return ret;
}

LL pow_mod(LL a, LL n, LL mod) {
    LL ret = 1;
    LL temp = a % mod;
    while(n) {
        if(n & 1)ret = mult_mod(ret, temp, mod);
        temp = mult_mod(temp, temp, mod);
        n >>= 1;
    }
    return ret;
}

bool check(LL a, LL n, LL x, LL t) {
    LL ret = pow_mod(a, x, n);
    LL last = ret;
    for(LL i = 1; i <= t; i++) {
        ret = mult_mod(ret, ret, n);
        if(ret == 1 && last != 1 && last != n - 1)return true;
        last = ret;
    }
    if(ret != 1)return true;
    else return false;
}

bool Miller_Rabin(LL n) {
    if( n < 2)return false;
    if( n == 2)return true;
    if( (n & 1) == 0)return false;
    LL x = n - 1;
    LL t = 0;
    while( (x & 1) == 0 ) {
        x >>= 1;
        t++;
    }
    srand(time(NULL));
    for(int i = 0; i < S; i++) {
        LL a  = rand() % (n - 1) + 1;
        if( check(a, n, x, t) )
            return false;
    }
    return true;
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        LL n;
        scanf("%llu", &n);
        if(n==4){
            printf("2 2\n");
        }
        for(LL  i = 3; i < n / 2; i+=2) {
            if(Miller_Rabin(i)) {
                LL q = n - i;
                if(Miller_Rabin(q)) {
                    printf("%llu %llu\n", i, q);
                    break;
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/acm_du/article/details/80053482