题意:给你一两个数m和n,它们分别是某对数A,B的gcd和lcm,让你求出一对使得A+B最小的A,B。

n/m的所有质因子中,一定有一部分是只在A中的,另一部分是只在B中的。

于是对n/m质因子分解后,dfs枚举在A中的质因子是哪些,在B中的是哪些,然后尝试更新答案即可。(因为相等的质因子只可能同时在A中或者在B中,而long long内的数不同的质因子数不超过14个)

注意特判n==m的情况。

#include<algorithm>
#include<cstdio>
#include<cstdlib>
#define N 5500
using namespace std;
typedef long long ll;
ll ct,cnt;
ll fac[N],num[N],fac2[N];
const int BASE[]={2,3,5,7,11,13,17,19,23};
ll Quick_Mul(ll a,ll p,ll MOD)
{
if(!p){
return 0;
}
ll ans=Quick_Mul(a,p>>1,MOD);
ans=(ans+ans)%MOD;
if((p&1ll)==1ll){
ans=ans+a%MOD%MOD;
}
return ans;
}
ll Quick_Pow(ll a,ll p,ll MOD)
{
if(!p){
return 1;
}
ll ans=Quick_Pow(a,p>>1,MOD);
ans=Quick_Mul(ans,ans,MOD);
if((p&1ll)==1ll){
ans=a%MOD*ans%MOD;
}
return ans;
}
bool test(ll n,ll a,ll d){
if(n==2){
return 1;
}
if(n==a){
return 0;
}
if(!(n&1)){
return 0;
}
while(!(d&1ll)){
d>>=1;
}
ll t=Quick_Pow(a,d,n);
if(t==1){
return 1;
}
while(d!=n-1ll && t!=n-1ll && t!=1ll){
t=Quick_Mul(t,t,n);
d<<=1;
}
return t==n-1ll;
}
bool Miller_Rabin(ll n){
if(n==1 || n==3825123056546413051ll){
return 0;
}
for(int i=0;i<9;++i){
if(n==(ll)BASE[i]){
return 1;
}
if(!test(n,(ll)BASE[i],n-1ll)){
return 0;
}
}
return 1;
}
ll pollard_rho(ll n,ll c){
ll i=1,k=2;
ll x=rand()%(n-1)+1;
ll y=x;
while(1){
i++;
x=(Quick_Mul(x,x,n)+c)%n;
ll d=__gcd((y-x+n)%n,n);
if(1ll<d &&d<n){
return d;
}
if(y==x){
return n;
}
if(i==k){
y=x;
k<<=1;
}
}
}
void find(ll n,int c){
if(n==1){
return;
}
if(Miller_Rabin(n)){
fac[ct++]=n;
return;
}
ll p=n;
ll k=c;
while(p>=n){
p=pollard_rho(p,c--);
}
find(p,k);
find(n/p,k);
}
ll n,m,A,B,ans;
void dfs(int cur,ll now){
if(now*m+n/now<ans){
ans=now*m+n/now;
A=now*m;
B=n/now;
}
for(int i=cur;i<cnt;++i){
dfs(i+1,now*fac2[i]);
}
}
int main(){
srand(233);
while(scanf("%lld%lld",&m,&n)!=EOF){
if(m==n){
printf("%lld %lld\n",m,n);
continue;
}
ans=9000000000000000000ll;
ct=0;
find(n/m,120);
sort(fac,fac+ct);
num[0]=1;
int k=1;
for(int i=1;i<ct;++i){
if(fac[i]==fac[i-1]){
++num[k-1];
}
else{
num[k]=1;
fac[k++]=fac[i];
}
}
cnt=k;
for(int i=0;i<cnt;++i){
fac2[i]=1;
for(int j=0;j<num[i];++j){
fac2[i]*=fac[i];
}
}
dfs(0,1);
printf("%lld %lld\n",min(A,B),max(A,B));
}
return 0;
}

【Pollard-rho算法】【DFS】poj2429 GCD & LCM Inverse的更多相关文章

  1. POJ2429 GCD & LCM Inverse pollard_rho大整数分解

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

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

  3. poj2429 GCD & LCM Inverse

    用miller_rabin 和 pollard_rho对大数因式分解,再用dfs寻找答案即可. http://poj.org/problem?id=2429 #include <cstdio&g ...

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

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

  5. [POJ 2429] GCD & LCM Inverse

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

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

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

  7. 初学Pollard Rho算法

    前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...

  8. Pollard Rho 算法简介

    \(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...

  9. Pollard Rho算法浅谈

    Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...

随机推荐

  1. 2017ACM暑期多校联合训练 - Team 1 1006 HDU 6038 Function (排列组合)

    题目链接 Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m ...

  2. mac终端配色

    1. 终端输入 ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" 2. brew installxz ...

  3. Double类型的数向上取整和向下取整

  4. php webshell常见函数

    0x1 直接在字符串变量后面加括号, 会调用这个函数: <?php $s = 'system'; $e = 'assert'; $s('whoami'); $e('phpinfo();'); 0 ...

  5. phinx:php数据库迁移

    Phinx使你的php app进行数据迁移的过程变得异常轻松,在五分钟之内你就可以安装好Phinx 并进行数据迁移. 特性 使用php代码进行数据迁移 部署模式下迁移 五分钟之内使用 不再担心数据库的 ...

  6. sqlalchemy更新和插入操作

    def save_app_info(self): try: # update app_info print(self.dicts) data = db_session.query(App_Info). ...

  7. openjudge-NOI 2.6-1944 吃糖果

    题目链接:http://noi.openjudge.cn/ch0206/1944/ 题解: 递推,题目中给出了很详细的过程,不讲解 #include<cstdio> int n; int ...

  8. TypeError: not all arguments converted during string formatting

    print ("So, you're 5r old, %r tall and %r heavy." % (age, height, weight)) print ("So ...

  9. Linux文件访问流程及磁盘inode和block总结

    Linux文件访问流程 inode是文件的唯一标识,文件名和inode的对应关系存放在上一级目录的block中:inode里有指向文件block的指针和文件的属性,从而通过block获得文件数据. 磁 ...

  10. Python学习笔记——数据结构和算法(二)

    1.字典中一个键映射多个值 可以使用collections中的defaultdict来实现,defalultdict接受list或者set为参数 from collections import def ...