Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
POJ 1811 Prime Test
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 32534 | Accepted: 8557 | |
Case Time Limit: 4000MS |
Description
Input
Output
Sample Input
2
5
10
Sample Output
Prime
2
/*遇上一个题目不同,这个题目是输出最小的质因子*/
#include<iostream>
using namespace std;
#include<cstdio>
#define S 10
#include<cstdlib>
#include<ctime>
#define ll long long
ll cas, maxz;
ll read()
{
ll ans=;char c;
c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='')
{
ans=ans*+c-'';
c=getchar();
}
return ans;
}
ll quick_mul_mod(ll a,ll b,ll c)//a*b%c
{
ll ret=;
a%=c;b%=c;
while(b)
{
if(b&)
{
ret+=a;
ret%=c;
b--;
}
a<<=;
a%=c;
b>>=;
}
return ret;
}
ll gcd(ll a,ll b)
{
if(a==) return ;
if(a<) return gcd(-a,b);
if(b==)
return a;
return gcd(b,a%b);
}
ll Pollard_rho(ll x,ll c)
{
ll x1=rand()%(x-)+;
ll x2=x1;
int i=,k=;
while()
{
i++;
x1=(quick_mul_mod(x1,x1,x)+c)%x;
ll d=gcd(x2-x1,x);
if(d!=&&d!=x) return d;
if(x2==x1) return x;
if(i==k)
{
x2=x1;
k+=k;
}
} }
ll quick_mod(ll a,ll b,ll c)//ji suan a^b%c
{
ll ans=;
a%=c;
while(b)
{
if(b&)
{
b--;
ans=quick_mul_mod(ans,a,c);
}
b>>=;
a=quick_mul_mod(a,a,c);
}
return ans;
}
bool Miller_rabin(ll n)
{
if(n==) return true;
if(n<=||!(n&)) return false;
ll u=n-,t=;
while(!(u&))
{
u>>=;
t++;
}
for(int i=;i<S;++i)
{
ll x=rand()%(n-)+;
x=quick_mod(x,u,n);
for(int i=;i<=t;++i)
{
ll y=quick_mul_mod(x,x,n);
if(y==&&x!=&&x!=n-)
return false;
x=y;
}
if(x!=) return false;
}
return true;
}
void findpri(ll n)
{
if(n<=) return;
if(Miller_rabin(n))
{
if(n!=)
maxz=min(maxz,n);
return;
}
ll p=n;
while(p==n)
p=Pollard_rho(p,rand()%(n-)+);
findpri(p);
findpri(n/p);
}
int main()
{
srand(time());
cas=read();
while(cas--)
{
maxz=(<<)-;/*不知道为什么这个赋初值的最大值,只有赋值为小于等于(1<<31)-1才对,我的maxz明明是long long型的啊*/
ll n=read();
findpri(n);
if(Miller_rabin(n))
printf("Prime\n");
else printf("%lld\n",maxz);
}
return ;
}
Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test的更多相关文章
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- 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 ...
- poj 1811 Prime Test 大数素数测试+大数因子分解
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 27129 Accepted: 6713 Case ...
- Miller&&Pollard POJ 1811 Prime Test
题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...
- POJ 1811 Prime Test 素性测试 分解素因子
题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的 素数与素性测试 素因子分解利用 ...
- POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)
Description Given a big integer number, you are required to find out whether it's a prime number. In ...
- POJ 1811 Prime Test
题意:对于一个大整数,判断是否质数,如果不是质数输出最小质因子. 解法:判断质数使用Miller-Rabin测试,分解质因子使用Pollard-Rho,Miller-Rabin测试用的红书模板,将测试 ...
随机推荐
- c语言学习笔记.内存管理.
内存: 每个程序的内存是分区的:堆区.栈区.静态区.代码区. 1.代码区:放置所有的可执行代码,包括main函数. 2.静态区:存放所有的全局变量和静态变量. 3.栈区:栈(stack),先进后出.存 ...
- linux学习记录.1.安装
最近想了想决定开始学习linux. 在百度了一番后开始了安装,虚拟机VirtualBox,ubuntu. 基于VirtualBox虚拟机安装Ubuntu图文教程: http://blog.csdn.n ...
- 福建工程学院寒假作业第一周G题
涨姿势题1 TimeLimit:1000MS MemoryLimit:128000KB 64-bit integer IO format:%lld 涨姿势题就是所谓的优化题,在组队赛中,队伍发现 ...
- cpu几种架构区别
转自:http://smilejay.com/2012/07/intel-procssor-architecture/ (1)x86 (IA-32,i386,x86-32,x32) x86是指基于In ...
- $fhqTreap$
- $fhqTreap$与$Treap$的差异 $fhqTreap$是$Treap$的非旋版本,可以实现一切$Treap$操作,及区间操作和可持久化 $fhqTreap$非旋关键在于分裂与合并$(Sp ...
- SpringMVC_HelloWorld_03
通过注解的方式实现一个简单的HelloWorld. 源码 一.新建项目 同SpringMVC_HelloWorld_01 不同的是springmvc配置文件的命名和路径,此处为src/springmv ...
- Mybatis Common Mapper文件
表名/条件/字段 都可以传入进去 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mappe ...
- Photon3Unity3D.dll 解析一
IPhotonPeerListener Photon客户端回调接口 1: //只要有来自Photon Server的事件就触发 2: public virtual void OnEvent( Eve ...
- set -o vi AIX下shell
set -o vi 再用esc+K键就可以使用上一条指令了 esc+kesc+j上下翻 ksh默认是emacs风格的.set -o emacs 在AIX下使用自己已经使用过的命令 在AIX下使用,默认 ...
- 转载:Logistic回归原理及公式推导
转载自:AriesSurfer 原文见 http://blog.csdn.NET/acdreamers/article/details/27365941 Logistic回归为概率型非线性回归模型,是 ...