POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

Prime Test

Time Limit: 6000MS
Memory Limit: 65536K

Total Submissions: 29193
Accepted: 7392

Case Time Limit: 4000MS

Description

Given a big integer number, you are required to find out whether it‘s a prime number.

Input

The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 2^54).

Output

For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N.

Sample Input

2

5

10

Sample Output

Prime

2

Source

POJ Monthly

题目大意:T组数据,对于输入的N,若N为素数,输出"Prime",否则输出N的最小素因子

思路:因为N的规模为2^54所以普通的素性判断果断过不了。要用Miller Rabin素数测试来做。

而若N不为素数,则需要对N进行素因子分解。因为N为大数,考虑用Pollar Rho整数分解来做。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define MAX_VAL (pow(2.0,60))
//miller_rabbin素性测试
//__int64 mod_mul(__int64 x,__int64 y,__int64 mo)
//{
//    __int64 t;
//    x %= mo;
//    for(t = 0; y; x = (x<<1)%mo,y>>=1)
//        if(y & 1)
//            t = (t+x) %mo;
//
//    return t;
//}

__int64 mod_mul(__int64 x,__int64 y,__int64 mo)
{
    __int64 t,T,a,b,c,d,e,f,g,h,v,ans;
    T = (__int64)(sqrt(double(mo)+0.5));
    t = T*T - mo;
    a = x / T;
    b = x % T;
    c = y / T;
    d = y % T;
    e = a*c / T;
    f = a*c % T;
    v = ((a*d+b*c)%mo + e*t) % mo;
    g = v / T;
    h = v % T;
    ans = (((f+g)*t%mo + b*d)% mo + h*T)%mo;
    while(ans < 0)
        ans += mo;
    return ans;
}
__int64 mod_exp(__int64 num,__int64 t,__int64 mo)
{
    __int64 ret = 1, temp = num % mo;
    for(; t; t >>=1,temp=mod_mul(temp,temp,mo))
        if(t & 1)
            ret = mod_mul(ret,temp,mo);

    return ret;
}

bool miller_rabbin(__int64 n)
{
    if(n == 2)
        return true;
    if(n < 2 || !(n&1))
        return false;
    int t = 0;
    __int64 a,x,y,u = n-1;
    while((u & 1) == 0)
    {
        t++;
        u >>= 1;
    }
    for(int i = 0; i < 50; i++)
    {
        a = rand() % (n-1)+1;
        x = mod_exp(a,u,n);
        for(int j = 0; j < t; j++)
        {
            y = mod_mul(x,x,n);
            if(y == 1 && x != 1 && x != n-1)
                return false;
            x = y;
        }
        if(x != 1)
            return false;
    }
    return true;
}
//PollarRho大整数因子分解
__int64 minFactor;
__int64 gcd(__int64 a,__int64 b)
{
    if(b == 0)
        return a;
    return gcd(b, a % b);
}

__int64 PollarRho(__int64 n, int c)
{
    int i = 1;
    srand(time(NULL));
    __int64 x = rand() % n;
    __int64 y = x;
    int k = 2;
    while(true)
    {
        i++;
        x = (mod_exp(x,2,n) + c) % n;
        __int64 d = gcd(y-x,n);
        if(1 < d && d < n)
            return d;
        if(y == x)
            return n;
        if(i == k)
        {
            y = x;
            k *= 2;
        }
    }
}

void getSmallest(__int64 n, int c)
{
    if(n == 1)
        return;
    if(miller_rabbin(n))
    {
        if(n < minFactor)
            minFactor = n;
        return;
    }
    __int64 val = n;
    while(val == n)
        val = PollarRho(n,c--);
    getSmallest(val,c);
    getSmallest(n/val,c);
}
int main()
{
    int T;
    __int64 n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d",&n);
        minFactor = MAX_VAL;
        if(miller_rabbin(n))
            printf("Prime\n");
        else
        {
            getSmallest(n,200);
            printf("%I64d\n",minFactor);
        }
    }
    return 0;
}
时间: 2024-07-31 14:29:29

POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】的相关文章

HDU1164_Eddy&amp;#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime

POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数测试】【Pollar Rho整数分解】

GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.

HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime

POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】

Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the num

POJ2429_GCD &amp;amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.

51nod 1106 质数检测(miller rabin 素数测试.)

1106 质数检测 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出N个正整数,检测每个数是否为质数.如果是,输出"Yes",否则输出"No". Input 第1行:一个数N,表示正整数的数量.(1 <= N <= 1000) 第2 - N + 1行:每行1个数(2 <= S[i] <= 10^9) Output 输出共N行,每行为 Yes 或 No. Input示例 5 2 3 4 5 6

Miller_Rabin大素数测试与Pollard_rho整数分解模版

#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; typedef __int64 LL; const int Times = 20; LL factor[100], l; LL gcd(LL a, LL b) { return b ? gcd(b, a%b):a; } LL add_mod(LL a, LL b,

HDU 3864 D_num Miller Rabin 质数判断+Pollard Rho大整数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给出一个数N(1<=N<10^18),如果N只有四个约数,就输出除1外的三个约数. 思路:大数的质因数分解只能用随机算法Miller Rabin和Pollard_rho,在测试多的情况下正确率是由保证的. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <c

Miller Rabbin素数测试

步骤 ①先写快速幂取模函数 ②MR算法开始 (1)传入两个参数一个是底数一个是n也就是幂数,如果n是一个合数那么可以判定,这个数一定不是素数 (2)然后开始寻找一个奇数的n去计算,如果最后满足a^d%n=1那么这个可能就是一个素数,然后再判断k=n-1(目前数学不好不明所以) (3)MR结束 ③编写check函数,传入一个参数.首先排除一些情况 (1)是2 3 7 61(int范围内完全可以判断的底数)如果是的话return true; (2)是偶数,1,3的倍数或5的倍数或7的倍数所有条件并起