Goldbach(数论,Miller_Rabin素数判定)

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

题目来源

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛

题意:给定一个大于2的偶数,将其拆成两个素数的和。

题解:因为数据量很大,打1e6的素数表完全不够用(而且可能玄学超时),那么就可以直接暴力枚举然后使用Miller_Rabin素数判定,复杂度log(n)。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<time.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
typedef unsigned long long ll;
using namespace std;

typedef  unsigned long long ll;
ll mul(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 Pow(ll a,ll b,ll n)
{
    ll result=1;
    ll base=a%n;
    while(b)
    {
        if(b&1) result=mul(result,base,n)%n;
        base=mul(base,base,n)%n;
        b=b>>1;
    }
    return result;
}
int isprime(ll n)
{
    if(n<=73)
    {
        int i;
        for(i=2; i*i<=n; i++)
            if(n%i==0)
                break;
        if(i*i>n)
            return 1;
        else
            return 0;
    }

    ll pan[10] = {2, 3, 5, 7,11,31,61,73,233,331};
    ll i;
    for(i=0; i<10; i++)
        if(Pow(pan[i],n-1,n)!=1)break;//if(pan[i]^(n-1)%n==1)this int is prime
    if(i==10)return 1;
    else return 0;
}
ll vis[1000005],prime[1000005],len;
void primeall()
{
    for(ll i=2;i<=1000000;i++)
        if(vis[i]==0)
        {
            prime[++len]=i;
            if(i*i<=1e6)
                for(ll j=i*i;j<=1000000;j+=i) vis[j]=1;
            else
                for(ll j=i*2;j<=1000000;j+=i) vis[j]=1;
        }
}

int main()
{
    primeall();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        scanf("%llu",&n);

        for(ll i=2;i<=1000000;i++)
        {
            if(isprime(i) && isprime(n-i))
            {
                printf("%llu %llu\n",i,n-i);
                break;
            }
        }
    }
    return 0;
}

3 5

题目来源

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/80053683