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

    题目来源

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

    这道题就是输出一个数的素数和,主要是要精简算法,优化

    打表发现可能的分解情况中,较小的那个素数非常小,最大不过在一万左右

    所以现在问题就变成了如何快速的判断一个数是否为素数

    要注意的是题目给的数非常大,即使用long long 存稍微算一下加法也会炸

    所以要用unsigned long long 运算,输出用 %llu

    代码如下:

    Miller-Rabin素数检测算法

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #define N 10000
     4 typedef unsigned long long ll;
     5 ll ModMul(ll a,ll b,ll n)//快速积取模 a*b%n
     6 {
     7  
     8     ll ans = 0;
     9     while(b)
    10     {
    11       if(b & 1)
    12         ans = (ans + a) % n;
    13       a = ( a + a ) % n;
    14       b >>= 1;
    15     }
    16     return ans;
    17 }
    18 ll ModExp(ll a,ll b,ll n)//快速幂取模 a^b%n
    19 {
    20     ll ans = 1;
    21     while(b)
    22     {
    23       if(b & 1)
    24         ans = ModMul(ans,a,n);
    25       a = ModMul(a,a,n);
    26       b >>= 1;
    27     }
    28     return ans;
    29 }
    30 bool miller_rabin(ll n)//Miller-Rabin素数检测算法
    31 {
    32     ll i,j,a,x,y,t,u,s = 10;
    33     if(n == 2)
    34     return true;
    35     if(n < 2 || !(n & 1))
    36     return false;
    37     for(t = 0,u = n-1;!(u & 1);t ++,u >>= 1);//n-1=u*2^t
    38     for(i = 0;i < s;i ++)
    39     {
    40       a = rand() % (n - 1) + 1;
    41       x = ModExp(a,u,n);
    42       for(j = 0;j < t;j ++)
    43       {
    44           y = ModMul(x,x,n);
    45           if(y == 1&&x != 1&&x != n - 1)
    46             return false;
    47           x = y;
    48       }
    49       if(x != 1)
    50         return false;
    51     }
    52     return true;
    53 }
    54 int main()
    55 {
    56     ll n,t;
    57     scanf("%llu",&t);
    58     while(t--)
    59     {
    60         scanf("%llu",&n);
    61         for(ll i = 1;i < N;i ++)
    62         if(miller_rabin(i) && miller_rabin(n - i))
    63         {
    64             printf("%llu %llu
    ",i,n - i);
    65             break;
    66         }
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    天啦噜!原来Chrome自带的开发者工具能这么用你知道么!
    JavaScript易错知识点整理
    2016年开源巨献:来自百度的71款开源项目
    前端js面试中的常见的算法问题
    详解JS跨域问题
    console 输出信息
    js身份证校验
    阻止页面回退
    pc端js常用方法
    js实现QQ、微信、新浪微博分享功能
  • 原文地址:https://www.cnblogs.com/lu1nacy/p/9972299.html
Copyright © 2011-2022 走看看