POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】
Memory Limit: 65536K
Accepted: 7392
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;
}
POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】的更多相关文章
- POJ2429_GCD & LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...
- HDU1164_Eddy'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 ...
- Miller Rabin素数检测与Pollard Rho算法
一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...
- Miller Rabin素数检测
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #inclu ...
- 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)
关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...
- C语言单元測试
C语言单元測试 对于敏捷开发来说,单元測试不可缺少,对于Java开发来说,JUnit非常好,对于C++开发,也有CPPUnit可供使用,而对于传统的C语言开发,就没有非常好的工具可供使用,能够找到的有 ...
- 与数论的厮守01:素数的测试——Miller Rabin
看一个数是否为质数,我们通常会用那个O(√N)的算法来做,那个算法叫试除法.然而当这个数非常大的时候,这个高增长率的时间复杂度就不够这个数跑了. 为了解决这个问题,我们先来看看费马小定理:若n为素数, ...
- 【数论基础】素数判定和Miller Rabin算法
判断正整数p是否是素数 方法一 朴素的判定
随机推荐
- 解决like '%字符串%'时索引不被使用的方法
解决like '%字符串%'时索引不被使用的方法 分步阅读 解决like '%字符串%'时索引不被使用的方法,如果like以通配符开头('%abc')时索引会失效会变成全表扫描的操作. 工具/原料 ...
- DocView mode 0 -- 介绍
DocView mode,可作为主模式也可以作为minor mode,可以用来阅读DVI(ps后缀),PDF,OpenDocument(libreoffice文档),微软的doc.支持截取 ...
- sql server 2008启动时:已成功与服务器建立连接,但是在登录过程中发生错误。(provider:命名管道提供程序,error:0-管道的另一端上无任何进程。)(Microsoft SQL Server,错误:233) 然后再连接:错误:18456
问题:sql server 2008启动时:已成功与服务器建立连接,但是在登录过程中发生错误.(provider:命名管道提供程序,error:0-管道的另一端上无任何进程.)(Microsoft S ...
- DefaultActionInvocation 源码
/** * Copyright 2002-2006,2009 The Apache Software Foundation. * * Licensed under the Apache License ...
- iossharesdk微信登录出错
只用下面的初始化就行了 // //添加微信应用 注册网址 http://open.weixin.qq.com // [ShareSDK connectWeChatWithAppId:mod ...
- Spoj-ANTP Mr. Ant & His Problem
Mr. Ant has 3 boxes and the infinite number of marbles. Now he wants to know the number of ways he c ...
- 洛谷 [P3150] pb的游戏
博弈论基础 本题可以视作P2148 E&D 的前置技能 本题直接判断奇偶性来求解, 证明就是2148 的证明 不贴代码
- 扰动法--*BZOJ3157: 国王奇遇记
求$\sum_{i=1}^ni^mm^i$.$n \leq 1e9,m \leq 200$. 其实我也不知道这东西为啥叫“扰动法”,大概是在黑暗的边缘试探?就是那种,人家再多一点就被您看破了,然后您就 ...
- Java面试题集(四)
二. Java Web基础部分 在js中如何创建一个对象? var p1={name:”tom”,”age”:12}; function Person(name,age){ this.name=nam ...
- Linux 系统的常用命令之 rm ,rm -rf , rm -f 以及rm 命令的其他参数命令
1.rm -rf * 删除当前目录下的所有文件,这个命令很危险,应避免使用. 所删除的文件,一般都不能恢复! 2.rm -f 其中的,f参数 (f --force ) 忽略不存在的文件,不显示任何信息 ...