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算 ...
随机推荐
- java 核心技术卷一笔记 6 .1接口 lambda 表达式 内部类
6.1 接口不是类,是对类的一组需求的描述,这些类需要遵守接口描述的统一格式进行定义.例如:Arrays类中sort方法(可以对对象数组进行排序)前提是对象所属的类必须实现了Comparable 接口 ...
- Shell脚本之for循环、while循环,if语句、case语句
1. for循环一般格式: 格式1: for((条件)) do 动作 done 格式2: for 变量名 in 范围 do 动作 done1234567891011121314实验:##1. 输出数字 ...
- House of Spirit(fastbin)
0x01 fastbin fastbin所包含chunk的大小为16 Bytes, 24 Bytes, 32 Bytes, … , 80 Bytes.当分配一块较小的内存(mem<=64 Byt ...
- CF-1111 (2019/2/7 补)
CF-1111 题目链接 A. Superhero Transformation tags : strings #include <bits/stdc++.h> using namespa ...
- 【动态规划】poj2353Ministry
拓扑序……好些玄妙 Description Mr. F. wants to get a document be signed by a minister. A minister signs a doc ...
- 5.电影搜索之 自动填充,也叫autocomplete、搜索建议!
什么叫自动填充,用过百度的应该都知道!当你输入关键词之后,会有一个下拉的候选列表,都是与你输入的内容相关的,这个就是自动填充的搜索建议.一般的搜索引擎或者站内搜索都会有这个功能. 今天分享下这个功能的 ...
- python--BOM和DOM
一. 介绍 什么是BOM和DOM? 简要答案:BOM是浏览器对象模型,用来获取或设置浏览器的属性.行为,例如:新建窗口.获取屏幕分辨率.浏览器版本号等. DOM是文档对象模型,用来获取或设置文档中标签 ...
- SqlServer 2014该日志未截断,因为其开始处的记录是挂起的复制操作或变更数据捕获
环境:AlwaysOn集群 操作系统:Windows Server 2008 R2 数据库: SQL Server 2014 错误提示:“该日志未截断,因为其开始处的记录是挂起的复制操作或变更数据捕获 ...
- Python基础知识-day2
格式化输出 %占位符,s字符串,d 数字, 表示% 用%% name = input("请输入姓名: ") age = input("请输入年龄: ") he ...
- vfs_caches_init函数解析
vfs_caches_init函数初始化VFS,下面梳理函数调用流程 start_kernel() -->vfs_caches_init_early(); -->dcache_init_e ...