zoukankan      html  css  js  c++  java
  • 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

    题目大意就是给你一个偶数,让你把偶数分成两个素数和,输出任意一组答案即可
    打表发现可能的分解情况中,较小的那个素数非常小,最大不过在一万左右
    所以现在问题就变成了如何快速的判断一个数是否为素数
    所以要用“Miller-Rabin素数检测算法”,具体参加如下博客
    https://blog.csdn.net/zengaming/article/details/51867240
    要注意的是题目给的数非常大,即使用long long存稍微算一下加法也会炸
    所以要用unsigned long long 运算,输出用 %llu

    #include <cstdio>
    #include <cstdlib>
    #include<iostream>
    #define N 10000
    using namespace std;
    typedef unsigned long long ll;
    ll ModMul(ll a,ll b,ll n)//快速积取模 a*b%n
    {
        ll ans=0;
          while(b)
          {
              if(b&1)
                ans=(ans+a)%n;
              a=(a+a)%n;
              b>>=1;
          }
          return ans;
    }
    ll ModExp(ll a,ll b,ll n)//快速幂取模 a^b%n
    {
          ll ans=1;
          while(b)
          {
              if(b&1)
                ans=ModMul(ans,a,n);
              a=ModMul(a,a,n);
              b>>=1;
          }
          return ans;
    }
    bool miller_rabin(ll n)//Miller-Rabin素数检测算法
    {
          ll i,j,a,x,y,t,u,s=10;
          if(n==2)
            return true;
          if(n<2||!(n&1))
            return false;
          for(t=0,u=n-1;!(u&1);t++,u>>=1);//n-1=u*2^t
          for(i=0;i<s;i++)
          {
              a=rand()%(n-1)+1;
              x=ModExp(a,u,n);
              for(j=0;j<t;j++)
              {
                  y=ModMul(x,x,n);
                  if(y==1&&x!=1&&x!=n-1)
                    return false;
                  x=y;
              }
              if(x!=1)
                return false;
          }
          return true;
    }
    
    int main()
    {
        ll n;
        ll t;
        scanf("%llu",&t);
          while(t--)
          {
              scanf("%llu",&n);
    
              for(ll i=1;i<N;i++)
              if(miller_rabin(i)&&miller_rabin(n-i))
              {
                  printf("%llu %llu
    ",i,n-i);
                  break;
              }
    
    
          }
          return 0;
    }
    View Code

    路漫漫其修远兮,吾将上下而求索
  • 相关阅读:
    win7 32位家庭版 加到4G内存后显示只有2G可用 的解决办法
    Extjs grid 获取json数据时报各种错误的原因(缺少分号,语法错误)
    css中文乱码
    3种分页储存过程
    腾讯微博SDK C#版本 发微博时有中文报check sign error的解决办法
    调用log4net.dll时报一大堆错误的情况
    QT SDK 4.7.4 在windows平台的发布问题
    Ouath 验证过程
    《转》Oracle EBS数据定义移植工具:FNDLOAD
    Oracle的锁表与解锁
  • 原文地址:https://www.cnblogs.com/tian-luo/p/8909209.html
Copyright © 2011-2022 走看看