zoukankan      html  css  js  c++  java
  • 计蒜客 Goldbach Miller_Rabin判别法(大素数判别法)

    题目链接:https://nanti.jisuanke.com/t/25985

    题目:

    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
    
    题意:哥德巴赫猜想:任意一个大于2的偶数即可表示为两个素数的和。运用Miller_Rabin判别法从n/2向两边遍历(由素数的分布可知这样时间复杂度更优)。
    代码实现如下:
     1 #include <cstdio>
     2 #include <ctime>
     3 #include <cstdlib>
     4 
     5 typedef long long ll;
     6 int t;
     7 long long n;
     8 
     9 ll multi(ll a, ll b, ll mod) {
    10     ll ret = 0;
    11     while(b) {
    12         if(b & 1)
    13             ret = ret + a;
    14         if(ret >= mod)
    15             ret -= mod;
    16 
    17         a = a + a;
    18         if(a >= mod)
    19             a -= mod;
    20         b >>= 1;
    21     }
    22     return ret;
    23 }
    24 ll quick_pow(ll a, ll b, ll mod) {
    25     ll ret = 1;
    26     while(b) {
    27         if(b & 1)
    28             ret = multi(ret, a, mod);
    29         a = multi(a, a, mod);
    30         b >>= 1;
    31     }
    32     return ret;
    33 }
    34 bool Miller_Rabin(ll n) {
    35     ll u = n - 1, pre, x;
    36     int i, j, k = 0;
    37     if(n == 2 || n == 3 || n == 5 || n == 7 || n  == 11)
    38         return true;
    39     if(n == 1 || (!(n % 2)) || (!(n % 3)) || (!(n % 5)) || (!(n % 7)) || (!(n % 11)))
    40         return false;
    41     for(; !(u & 1); k++, u >>= 1);
    42     srand(time(NULL));
    43     for(i = 0; i < 5; i++) {
    44         x = rand() % (n - 2) + 2;
    45         x = quick_pow(x, u, n);
    46         pre = x;
    47         for(j = 0; j < k; j++) {
    48             x = multi(x, x, n);
    49             if(x == 1 && pre != 1 && pre != (n - 1))
    50                 return false;
    51             pre = x;
    52         }
    53         if(x != 1)
    54             return false;
    55     }
    56     return true;
    57 }
    58 
    59 int main() {
    60     scanf("%d", &t);
    61     while(t--) {
    62         scanf("%lld", &n);
    63         long long k = n /2;
    64         if(Miller_Rabin(k)) {
    65             printf("%lld %lld
    ", k, k);
    66         } else {
    67             long long l = k - 1, r = k + 1;
    68             while(!Miller_Rabin(l) || !Miller_Rabin(r)) {
    69                 l--;
    70                 r++;
    71             }
    72             printf("%lld %lld
    ", l, r);
    73         }
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    Microsoft .NET Framework 以及 CLR 的版本
    如何:备份 Team Foundation Server
    通过VS2008SP1 访问TFS2010
    Project 2007 Understanding Project's Percent Complete vs. Percent Work Complete
    TFS:从单服务器部署移到双服务器部署
    C#网络编程(基本概念和操作) Part.1
    图解Windows server 2012故障转移群集的安装、建立
    产品经理经验总结
    TCP同步与异步及阻塞模式,多线程+阻塞模式,非阻塞模式简单介绍
    Windows Phone开发概论
  • 原文地址:https://www.cnblogs.com/Dillonh/p/9301991.html
Copyright © 2011-2022 走看看