标签:des style blog http io ar color os sp
素数的测试:Miller测试进行k次,将合数当成素数处理的错误概率最多不会超过4^(-k).
参考博客:http://blog.sina.com.cn/s/blog_6f71bea30100okag.html
PS:需要人品啊。
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 29406 | Accepted: 7479 | |
| Case Time Limit: 4000MS | ||
Description
Input
Output
Sample Input
2 5 10
Sample Output
Prime 2
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <time.h>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
///#define LL long long
#define LL __int64
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007
#define MAX (pow(2.0, 60))
#define TIME 12
#define C 240
const int maxn = 110;
using namespace std;
LL Min;
LL gcd(LL x, LL y)
{
    if(y == 0) return x;
    return gcd(y, x%y);
}
LL mod_mult(LL a, LL b, LL n) ///计算(a*b) mod n
{
    LL s = 0;
    a = a % n;
    while (b)
    {
        if (b & 1)
        {
            s += a;
            if (s >= n)
                s -= n;
        }
        a = a << 1;
        if (a >= n)
            a -= n;
        b = b >> 1;
    }
    return s;
}
LL mod_exp(LL a, LL b, LL n) ///计算(a^b) mod n
{
    LL d = 1;
    a = a % n;
    while (b >= 1)
    {
        if (b & 1) d = mod_mult(d, a, n);
        a = mod_mult(a, a, n);
        b = b >> 1;
    }
    return d;
}
bool Wintess(LL a, LL n) ///以a为基对n进行Miller测试并实现二次探测
{
    LL m, x, y;
    int i, j = 0;
    m = n - 1;
    while (m % 2 == 0) ///计算(n-1)=m*(2^j)中的j和m,j=0时m=n-1,不断的除以2直至n为奇数
    {
        m = m >> 1;
        j++;
    }
    x = mod_exp(a, m, n);
    for (i = 1; i <= j; i++)
    {
        y = mod_exp(x, 2, n);
        if ((y == 1) && (x != 1) && (x != n - 1)) ///二次探测
            return true; ///返回true时,n是合数
        x = y;
    }
    if (y != 1)
        return true;
    return false;
}
bool miller_rabin(int times,LL n) ///对n进行s次的Miller测试
{
    LL a;
    int i;
    if (n == 1)
        return false;
    if (n == 2)
        return true;
    if (n % 2 == 0)
        return false;
    srand(time(NULL));
    for (i = 1; i <= times; i++)
    {
        a = rand() % (n - 1) + 1;
        if (Wintess(a, n))
            return false;
    }
    return true;
}
LL Pollard(LL n, int c) ///对n进行因字分解,找出n的一个因子,注意该因子不一定是最小的
{
    LL i, k, x, y, d;
    srand(time(NULL));
    i = 1;
    k = 2;
    x = rand() % n;
    y = x;
    while (true)
    {
        i++;
        x = (mod_mult(x, x, n) + c) % n;
        d = gcd(y - x, n);
        if (d > 1 && d < n)
            return d;
        if (y == x) ///该数已经出现过,直接返回即可
            return n;
        if (i == k)
        {
            y = x;
            k = k << 1;
        }
    }
}
void get_small(LL n, int c) ///找出最小的素数因子
{
    LL m;
    if(n == 1) return;
    if(miller_rabin(TIME, n))
    {
        if(n < Min) Min = n;
        return;
    }
    m = n;
    ///while(m == n) m = Pollard(n, c--);
    while (m == n) //找出n的一个因子
        m = Pollard(n, c--);
    get_small(m, c);
    get_small(n/m, c);
}
int main()
{
    int T;
    LL n;
    cin >>T;
    while(T--)
    {
        scanf("%I64d",&n);
        Min = MAX;
        if(miller_rabin(TIME, n))
        {
            puts("Prime");
            continue;
        }
        get_small(n, C);
        printf("%I64d\n",Min);
    }
    return 0;
}
POJ 1811 Prime Test(费马小定理+二次探测定理)
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/xu12110501127/article/details/41358697