POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)

【题目链接】 http://poj.org/problem?id=2429

【题目大意】

  给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小

【题解】

  我们发现,(x/gcd)*(y/gcd)=lcm/gcd,并且x/gcd和y/gcd互质
  那么我们先利用把所有的质数求出来Pollard_Rho,将相同的质数合并
  现在的问题转变成把合并后的质数分为两堆,使得x+y最小
  我们考虑不等式a+b>=2sqrt(ab),在a趋向于sqrt(ab)的时候a+b越小
  所以我们通过搜索求出最逼近sqrt(ab)的值即可。

【代码】

#include <cstdio>
#include <algorithm>
#include <cmath>
#define C 2730
#define S 3
using namespace std;
typedef long long ll;
ll n,m,s[1000],cnt,f[1000],cnf,ans;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mul(ll a,ll b,ll n){return(a*b-(ll)(a/(long double)n*b+1e-3)*n+n)%n;}
ll pow(ll a, ll b, ll n){
    ll d=1; a%=n;
    while(b){
        if(b&1)d=mul(d,a,n);
        a=mul(a,a,n);
        b>>=1;
    }return d;
}
bool check(ll a,ll n){
    ll m=n-1,x,y;int i,j=0;
    while(!(m&1))m>>=1,j++;
    x=pow(a,m,n);
    for(i=1;i<=j;x=y,i++){
        y=pow(x,2,n);
        if((y==1)&&(x!=1)&&(x!=n-1))return 1;
    }return y!=1;
}
bool miller_rabin(int times,ll n){
    ll a;
    if(n==1)return 0;
    if(n==2)return 1;
    if(!(n&1))return 0;
    while(times--)if(check(rand()%(n-1)+1,n))return 0;
    return 1;
}
ll pollard_rho(ll n,int c){
    ll i=1,k=2,x=rand()%n,y=x,d;
    while(1){
        i++,x=(mul(x,x,n)+c)%n,d=gcd(y-x,n);
        if(d>1&&d<n)return d;
        if(y==x)return n;
        if(i==k)y=x,k<<=1;
    }
}
void findfac(ll n,int c){
    if(n==1)return;
    if(miller_rabin(S,n)){
        s[cnt++]=n;
        return;
    }ll m=n;
    while(m==n)m=pollard_rho(n,c--);
    findfac(m,c),findfac(n/m,c);
}
void dfs(int pos,long long x,long long k){
    if(pos>cnf)return;
    if(x>ans&&x<=k)ans=x;
    dfs(pos+1,x,k);
    x*=f[pos];
    if(x>ans&&x<=k)ans=x;
    dfs(pos+1,x,k);
}
int main(){
    while(~scanf("%lld%lld",&m,&n)){
        if(n==m){printf("%lld %lld\n",n,n);continue;}
        cnt=0; long long k=n/m;
        findfac(k,C);
        sort(s,s+cnt);
        f[0]=s[0]; cnf=0;
        for(int i=1;i<cnt;i++){
            if(s[i]==s[i-1])f[cnf]*=s[i];
            else f[++cnf]=s[i];
        }long long tmp=(long long)sqrt(1.0*k);
        ans=1; dfs(0,1,tmp);
        printf("%lld %lld\n",m*ans,k/ans*m);
    }return 0;
}

  

时间: 2024-08-05 19:31:27

POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)的相关文章

POJ 2429 GCD &amp; LCM Inverse (大数分解)

GCD & LCM Inverse 题目:http://poj.org/problem?id=2429 题意: 给你两个数的gcd和lcm,[1, 2^63).求a,b.使得a+b最小. 思路: lcm = a * b / gcd 将lcm/gcd之后进行大数分解,形成a^x1 * b^x2 * c^x3-- 的形式,其中a,b,c为互不相同的质数.然后暴力枚举即可. 代码: #include<map> #include<set> #include<queue>

poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9928   Accepted: 1843 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a a

poj 2429 GCD &amp; LCM Inverse miller_rabin素数判定和pollard_rho因数分解

题意: 给gcd(a,b)和lcm(a,b),求a+b最小的a和b. 分析: miller_rabin素数判定要用费马小定理和二次探测定理.pollard_rho因数分解算法导论上讲的又全又好,网上的资料大多讲不清楚. 代码: //poj 2429 //sep9 #include <iostream> #include <map> #include <vector> #define gcc 10007 #define max_prime 200000 using nam

POJ 2429 GCD &amp; LCM Inverse

设答案为ans1,ans2 ans1=a1*gcd,ans2=a2*gcd,a1,a2互质 gcd*a1*b1=lcm,gcd*a2*b2=lcm a1*b1=lcm=(ans1*ans2)/gcd=a1*a2 综上所诉,a1=b2,a2=b1. 也就是说,ans1=gcd*k1,ans2=gcd*k2 要求k1,k2尽量接近,并且k1,k2互质,并且,k2*k2=lcm/gcd 需要用到Pollard_rho分解质因数,然后暴力搜索寻找k1,k2.用了kuangbin大神的Pollard_rh

POJ 2429 GCD &amp;amp; LCM Inverse (大数分解)

GCD & LCM Inverse 题目:http://poj.org/problem? id=2429 题意: 给你两个数的gcd和lcm,[1, 2^63). 求a,b.使得a+b最小. 思路: lcm = a * b / gcd 将lcm/gcd之后进行大数分解.形成a^x1 * b^x2 * c^x3-- 的形式.当中a,b,c为互不同样的质数.然后暴力枚举就可以. 代码: #include<map> #include<set> #include<queue&

POJ 1979 Red and Black(简单DFS)

Red and Black Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he

POJ 2679:Adventurous Driving(SPFA+DFS)

http://poj.org/problem?id=2679 Adventurous Driving Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1596   Accepted: 455 Description After a period of intensive development of the transportation infrastructure, the government of Ruritania

POJ5429 GCD &amp; LCM Inverse

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9913   Accepted: 1841 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a a

POJ 1426 Find The Multiple(寻找倍数)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom