关于4/22/2018宁夏网络赛 B题 Goldbach

Goldbach

又划了一手水

是一道素数判定,题目范围是2^63,当时用了弥勒罗宾算法来判定素数,确实挺快,但是一直卡在4e19,弄吧明白为什么,还以为是5e19周围没有素数,又用3分去找,直到快结束才想到ULL,LL会越界。。很僵硬


题目:

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.

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

题意就是素数哥德巴赫猜想,判断两个素数

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef unsigned long long ll;
using namespace std;

ll add_mod(ll a,ll b,ll mod){    
    ll ans=0;                    
    while(b){                    
        if(b&1)                    
            ans=(ans+a)%mod;
        a=a*2%mod;
        b>>=1;
    }
    return ans;
}

ll pow_mod(ll a,ll n,ll mod){           
    if(n>1){
        ll tmp=pow_mod(a,n>>1,mod)%mod;
        tmp=add_mod(tmp,tmp,mod);
        if(n&1) tmp=add_mod(tmp,a,mod);
        return tmp;
    }
    return a;
}

bool Miller_Rabbin(ll n,ll a){
    ll d=n-1,s=0,i;
    while(!(d&1)){            
        d>>=1;
        s++;
    }
    ll t=pow_mod(a,d,n);   
    if(t==1 || t==-1)       
        return 1;
    for(i=0;i<s;i++){           
        if(t==n-1)           
            return 1;
        t=add_mod(t,t,n);   
    }
    return 0;
}

bool is_prime(ll n){
    ll i,tab[4]={3,4,7,11};
    for(i=0;i<4;i++){              
        if(n==tab[i])
            return 1;        
        if(!n%tab[i])
            return 0;
        if(n>tab[i] && !Miller_Rabbin(n,tab[i]))
            return 0;
    }
    return 1;
}

int main(){
    int n;
    cin>>n;
    while(n--)
    {
        long long s;
        scanf("%lld",&s);
        ll l=s/2;
        	for(ll i = 2;i<=l;i++)
       	 	{
        		if(is_prime(i)&&is_prime(s-i))
        		{
        			printf("%lld %lld\n",i,s-i);
        			break;
        		}
       		}
        
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/amous_x/article/details/80073415