/*
题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子
Miller Rabin +Poller Rho 大素数判定+大数找质因子
后面这个算法嘛 基于Birthday Paradox
简单点说就是 在 1到100 内去一个数 ai ai==42的概率很小
但是如果取两个数 ai bi ai-bi==42 的概率就会变大
应用到找素因子上 就不用像试除法那样一个一个的试
但是如果枚举ai bi 显然也很slow 那么有一个非常好使(奇怪)的函数
f(x)=x*x+c 这样将x和f(x)%p作为两个数 看看x-f(x) 与p的关系
这里我们不是试试 p%(x-f(x))==0来找 而是 gcd一下 d=(p,(x-f(x)))
d显然是p的因子 然后分解d 这样就ok了 */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#define ll long long
using namespace std;
ll T,ans[],tot;
ll init()
{
ll x=;char s;s=getchar();
while(s<''||s>'')s=getchar();
while(s>=''&&s<=''){x=x*+s-'';s=getchar();}
return x;
}
ll gcd(ll a,ll b)
{
return !b?a:gcd(b,a%b);
}
ll slow_mul(ll a,ll b,ll c)//防止爆掉
{
a=a%c;b=b%c;
ll an=;
while(b)
{
if(b&)
{
b--;
an=an+a;
an=an%c;
}
a<<=;a=a%c;b>>=;
}
return an;
}
ll Mi(ll a,ll m,ll p)
{
if(m==)return ;
ll x=Mi(a,m/,p)%p;
x=slow_mul(x,x,p);
if(m&)x=slow_mul(x,a,p);
return x;
}
ll Pollard_rho(ll p,ll c)
{
ll i=,k=;
ll x=rand()%(p-)+;
ll fx=x;
while()
{
i++;
x=(slow_mul(x,x,p)+c)%p;
ll g=gcd(fx-x+p,p);//防止fx-x<0
if(g!=&&g!=p)return g;//找到一个因子
if(x==fx)return p;//进入环 换一个随机数
if(i==k)
{
k=k+k;
fx=x;
}
}
}
bool Miller_Rabin(ll n)
{
if(n==)return ;
if(n==||!(n&))return ;
ll m=n-,j=;
while(!(m&))
{
j++;
m=m>>;
}
//srand(unsigned(time(0)));丫丫的poj用srand会RE 我看了好久0.0
for(int i=;i<=;i++)
{
ll a=rand()%(n-)+;
ll x=Mi(a,m,n);
for(int k=;k<=j;k++)
{
ll y=slow_mul(x,x,n);
if(y==&&x!=&&x!=n-)return ;
x=y;
}
if(x!=)return ;
}
return ;
}
void findpri(ll n)//质因数分解n
{
if(n<=)return;
if(Miller_Rabin(n))
{
ans[++tot]=n;
return ;
}
ll p=n;
while(p==n)//可能生成的随机数不合适
p=Pollard_rho(p,rand()%(n-)+);//返回n的一个因子
findpri(p);
findpri(n/p);
}
int main()
{
T=init();
while(T--)
{
ll n=init();
if(Miller_Rabin(n))//素数先来个判断
{
printf("Prime\n");
continue;
}
memset(ans,,sizeof(ans));//所有质因子
tot=;
findpri(n);//找质因子
ll an=ans[];
for(int i=;i<=tot;i++)
an=min(an,ans[i]);
printf("%lld\n",an);
}
return ;
}

poj 1811 Pallor Rho +Miller Rabin的更多相关文章

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

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

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

    GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...

  3. Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法

    BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1044  Solved: 322[Submit][ ...

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

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  5. POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)

    题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...

  6. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

  7. 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 ...

  8. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

  9. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

随机推荐

  1. HDU 1069 Monkey and Banana(LIS最长上升子序列)

    B - LIS Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descripti ...

  2. How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code = 0) ?

    How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code ...

  3. run_command函数分析

    一.概述 位置:common/main.c 功能:根据传入参数(命令),在命令存储区(.u_boot_cmd)中查找对应的命令,找到命令并调用对应的函数执行 流程: 二.分析 1.函数说明信息 /** ...

  4. [转]Aggregate tasks i Sharepoint 2013

    from http://sharepoint247.com/mysite/aggregate-tasks-i-sharepoint-2013/ Aggregate tasks i Sharepoint ...

  5. 【HDOJ】1483 Automatic Correction of Misspellings

    水模拟题. /* 1483 */ #include <cstdio> #include <cstring> #include <cstdlib> #define M ...

  6. 去掉cell边框的简单办法

    实很简单,把backgroundView设置为一个空的View,然后就干净了 UIView *tempView = [[UIView alloc] init]; [cell setBackground ...

  7. 数据结构(脑洞题,BIT):COGS 2394. 比赛

    比赛 时间限制:1 s   内存限制:256 MB [题目描述] n(n≤100000)个人编号为0到n-1,每人都有一个能力值,大小在0到n-1之间,各不相同,他们之间有c场比赛,每场比赛指定一个区 ...

  8. 数学:UVAoj 11174 Stand in a Line

    Problem J Stand in a Line Input: Standard Input Output: Standard Output All the people in the bytela ...

  9. vector::erase returns incompatible iterator in debug build

    关于std::vector中erase的用法http://www.cplusplus.com/reference/vector/vector/erase/ #include <vector> ...

  10. 光学字符识别OCR

    1.功能: 光学字符识别(OCR,Optical Character Recognition)是指对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程 2.典型应用: 名片扫描 3 ...