miller_rabin + pollard_rho模版
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#define MAXN 100000
using namespace std;
typedef unsigned long long LL;
LL fac[MAXN],cnt,G,L,m,p;
LL min(LL a,LL b)
{
return a<b?a:b;
}
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
LL mult_mod(LL a,LL b,LL mod)
{
LL ans=0;
while(b)
{
if(b&1)
ans=(ans+a)%mod;
a=(a<<1)%mod;
b>>=1;
}
return ans;
}
LL pow_mod(LL a,LL b,LL mod)
{
LL d=1;
a%=mod;
while(b)
{
if(b&1)
d=mult_mod(d,a,mod);
a=mult_mod(a,a,mod);
b>>=1;
}
return d%mod;
}
bool witness(LL a,LL n)
{
LL u=n-1,t=0;
while((u&1)==0)
{
u>>=1;
t++;
}
LL x,x0=pow_mod(a,u,n);
for(LL i=1; i<=t; i++)
{
x=mult_mod(x0,x0,n);
if(x==1&&x0!=1&&x0!=(n-1))
return true;
x0=x;
}
if(x!=1)
return true;
return false;
}
bool miller_rabin(LL n)
{
if(n==2) return true;
if(n<2||!(n&1)) return false;
for(int j=1; j<=8; j++)
{
LL a=rand()%(n-1)+1;
if(witness(a,n))
return false;
}
return true;
}
LL pollard_rho(LL n,LL c)
{
LL i=1,k=2,d,x=2,y=2;
while(true)
{
i++;
x=(mult_mod(x,x,n)+c)%n;
d=gcd(y-x,n);
if(d!=1&&d!=n)
return d;
if(x==y) return n;
if(i==k)
{
y=x;
k<<=1;
}
}
}
void find_fac(LL n,LL c)
{
if(miller_rabin(n)||n<=1)
{
fac[cnt++]=n;
return;
}
LL p=pollard_rho(n,c);
while(p>=n) p=pollard_rho(p,c--);
find_fac(p,c);
find_fac(n/p,c);
}
void dfs( LL step,LL num)
{
if(step==cnt||num>p)
{
if(num<=p&&num>m)
m=num;
return;
}
dfs(step+1,num*fac[step]);
dfs(step+1,num);
}
int main()
{
srand(time(NULL));
while(scanf("%I64u%I64u",&G,&L)!=EOF)
{
cnt=0;
find_fac(L/G,181);
sort(fac,fac+cnt);
LL i=0,t=0;
while(i<cnt)
{
LL ans=1,j=i;
while(j<cnt&&fac[i]==fac[j])
{
ans*=fac[i];
j++;
}
fac[t++]=ans;
i=j;
}
cnt=t,m=1,p=sqrt(0.0+(L/G));
dfs(0,1);
printf("%I64u %I64u\n",m*G,L/m);
}
return 0;
}
miller_rabin + pollard_rho模版的更多相关文章
- 数论算法 剩余系相关 学习笔记 (基础回顾,(ex)CRT,(ex)lucas,(ex)BSGS,原根与指标入门,高次剩余,Miller_Rabin+Pollard_Rho)
注:转载本文须标明出处. 原文链接https://www.cnblogs.com/zhouzhendong/p/Number-theory.html 数论算法 剩余系相关 学习笔记 (基础回顾,(ex ...
- bzo4802 欧拉函数 miller_rabin pollard_rho
欧拉函数 Time Limit: 5 Sec Memory Limit: 256 MBSubmit: 1112 Solved: 418[Submit][Status][Discuss] Descr ...
- 随机算法 - Miller_Rabin pollard_rho
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- 计蒜客 18487.Divisions-大数的所有因子个数-Miller_Rabin+Pollard_rho-超快的(大数质因解+因子个数求解公式) (German Collegiate Programming Contest 2015 ACM-ICPC Asia Training League 暑假第一阶段第三场 F)
这一场两个和大数有关的题目,都用到了米勒拉宾算法,有点东西,备忘一下. 题目传送门 F. Divisions 传送门 这个题是求一个数的所有因子个数,但是数据比较大,1e18,所以是大数的题目,正常的 ...
- BZOJ4802 欧拉函数 数论
原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html 题目传送门 - BZOJ4802 题意概括 Description 已知N,求phi(N) ...
- PLAN OF HEOI(unfinished)
Au:整体二分/计算几何/多项式/fwtAg:可持久化重量平衡树/线段树分治/线段树合并/最短路树/最短路DAGCu:三分Up:博弈论/置换群/杜教筛/矩阵树定理/BSGS/动态树分治/网络流(线性规 ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- HDU-3864 D_num Miller_Rabin和Pollard_rho
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给定一个数n,求n的因子只有四个的情况. Miller_Rabin和Pollard_rho ...
- pollard_rho和Miller_Rabin
Miller_Rabin就是以概论大小来判断素数 可以判断2^63范围的数 pollard_rho推荐两个很好的博客来理解:整数分解费马方法以及Pollard rho和[ZZ]Pollard Rho算 ...
随机推荐
- codevs 5462 HYY迎亲I
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 青铜 Bronze 题目描述 Description HYY要娶山那头的JCH,可毕竟是山路,十分崎岖,他又十分的单(bai)纯(ch ...
- scanf("%s",s)与gets(s)
#include <stdio.h> void fun(char s[]) {; while(s[i]!='\0') {i++;} printf("%d",i);} v ...
- 更新Svn客户端后,右键菜单中没有TortoiseSVN
环境: OS: Windows XP sp3 升级后SVNServer: VisualSVN Server 2.7.3 升级后SVNClient: 小乌龟: ...
- 立个单调栈flag
http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=687&pid=1002
- 伪题解 洛谷 P1363 幻想迷宫(DFS)
毒瘤题,做了一晚上抄题解A了 因为是抄题解,我也不好意思说什么了,就发篇博客纪念一下吧 #include<iostream> #include<cstring> #includ ...
- Python学习笔记3(字典)
创建字典 dict函数 字典的格式化字符串 字典方法 clear copy fromkeys 序列是一个按照一定顺序将值进行组织的数据结构形式,可以通过索引对其进行征引.另外还有一种数据结构是通过名字 ...
- lsof指令使用简介
lsof替代了netstat和ps的全部工作.它可以带来那些工具所能带来的一切,而且要比那些工具多得多 最重要的是,当你给它传递选项时,默认行为是对结果进行“或”运算.因此,如果是用-i来拉出一个端口 ...
- laravel模型关联与列表展示
上面这个是一个模型关联的图,其实我们很容易去理解 比如说,一对一,也就是说一个用户对应的是一个手机号. 一对多,比如说一篇文章可以有多条评论 一对多反向:如一篇文章可以有多条评论,但对应每条评论也只针 ...
- Cassandra 数据库安装部署
安装版本 cassandra-3.11.4 系统版本 more /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 准备工作 Cassan ...
- (转) 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程(精)
原文地址:http://blog.csdn.net/holydancer/article/details/9219333 概述: 苹果的证书繁锁复杂,制作管理相当麻烦,今天决定重置一个游戏项目中的所有 ...