基本上一个裸的Miller_Rabin大素数判定和一个裸的Pollard_rho素数分解算法,当模板用吧!

 #include<cstdio>
#include<algorithm>
#include<stdlib.h>
#define LL long long
using namespace std;
const int s=;
int tol;
LL factor[]; LL mult_mod(LL a,LL b,LL c)//计算a*b%c;
{
LL ret=;
a%=c;
b%=c;
while(b>)
{
if(b&) ret=(ret+a)%c;
a<<=;
if(a>=c) a%=c;
b>>=;
}
return ret;
} LL pow_mod(LL a,LL b,LL mod)//计算a^b%b
{
if(b==) return a%mod;
a%=mod;
LL tmp=a;
LL ret=;
while(b>)
{
if(b&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
b>>=;
}
return ret;
} //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(LL a,LL n,LL x,LL t)
{
LL ret=pow_mod(a,x,n);
LL last=ret;
for(int i=; i<=t; i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return ;
last=ret;
}
if(ret!=) return ;
return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;
bool Miller_Rabin(LL a)
{
if(a<) return ;
if(a==) return ;
if((a&==)) return ;
LL x=a-;
LL t=;
while((x&)==)
{
x>>=;
t++;
}
for(int i=; i<s; i++)
{
long long b=rand()%(a-)+;
if(check(b,a,x,t))
return ;
}
return ;
} LL gcd(LL a,LL b)
{
if(a==)return ;
if(a<) return gcd(-a,b);
while(b)
{
LL t=a%b;
a=b;
b=t;
}
return a;
} LL Pollard_rho(LL x,LL c)
{
LL i=,k=;
LL x0=rand()%x;
LL y=x0;
while()
{
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k)
{
y=x0;
k+=k;
}
}
} void findfac(LL n)
{
if(Miller_Rabin(n))//素数
{
factor[tol++]=n;
return;
}
LL p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-)+);
findfac(p);
findfac(n/p);
} int main()
{
LL a;
int n;
scanf("%d",&n);
while(n--)
{
scanf("%lld",&a);
if(Miller_Rabin(a))
{
puts("Prime");
continue;
}
tol=;
findfac(a);
LL ans=factor[];
for(int i=;i<tol;i++)
if(factor[i]<ans)
ans=factor[i];
printf("%lld\n",ans);
}
return ;
}

poj 1811 Prim test的更多相关文章

  1. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  2. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

  3. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  4. POJ 2421(prim)

    http://poj.org/problem?id=2421 这个题和poj1258是一样的,只要在1258的基础上那么几行代码,就可以A,水. 题意:还是n连通问题,和1258不同的就是这个还有几条 ...

  5. Poj(1251),Prim字符的最小生成树

    题目链接:http://poj.org/problem?id=1251 字符用%s好了,方便一点. #include <stdio.h> #include <string.h> ...

  6. POJ 2031 prim

    Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...

  7. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  8. poj 1811 大数分解

    模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...

  9. poj 1811 Pallor Rho +Miller Rabin

    /* 题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子 Miller Rabin +Poller Rho 大素数判定+大数找质因子 后面这个算法嘛 基于Birthday Pa ...

随机推荐

  1. tcpdf MultiCell line break

    在程序中,我遇到MultiCell中显示三个字符串,开始时 $pdf->MultiCell(63.5, 30, $name."\n".$address."\n&qu ...

  2. 使用 Spring 2.5 基于注解驱动的 Spring MVC

    http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...

  3. Hql 中实用查询时候 引号的使用

    出错代码://List vlist = this.getHibernateTemplate().find("from AndroidCustomer ct where ct.token = ...

  4. C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式)

    学习书籍: <C#本质论> 1--C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是 ...

  5. .Net规则引擎Biztalk,Workflow和CKRule的比较

    微软的规则引擎   很多朋友会问,.Net平台有没有规则引擎?像Java就有很多的规则引擎,Drools,ILog都做得非常好,但.Net好像还没有哦.很多知道规则引擎,但不知道.Net的朋友都烦这个 ...

  6. Oracle PL/SQL 事物处理 银行转账

    Oracle数据库中的事务处理:添加,修改,删除时需要使用事务处理(显示事务). 1.事务的分类显示事务(添加,修改,删除)和隐式事务(除了添加,修改,删除). 2.事务的执行方式:自动提交(jdbc ...

  7. CruiseControl.NET : Configuration Preprocessor

    Original link: http://build.sharpdevelop.net/ccnet/doc/CCNET/Configuration%20Preprocessor.html http: ...

  8. vc实现ping

    //ping.h #ifndef _CPING_H_ #define _CPING_H_ #include <Winsock2.h> #include <Windows.h> ...

  9. <<深入Java虚拟机>>-第二章-Java内存区域-学习笔记

    Java运行时内存区域 Java虚拟机在运行Java程序的时候会将它所管理的内存区域划分为多个不同的区域.每个区域都有自己的用途,创建以及销毁的时间.有的随着虚拟机的启动而存在,有的则是依赖用户线程来 ...

  10. JavaScript的事件监听、捕获和冒泡

    在前端开发中,我们经常需要对某些事件进行监听.这样只要在指定的元素上触发了该事件,就会执行一个回调函数来进行相关的操作. 而JavaScript中事件监听的方法总共有三种,分别如下: element. ...