POJ 2429 GCD & amp; LCM Inverse pollard_rov big factor decomposition

Source: Internet
Author: User

This question. Assume that the original numbers are a and B, after the GCD and LCM are given, we only need to ask the LCM/GCD factor to find the/gcd and B/gcd, and then we can find that there cannot be a common number between the two things. it is necessary to take all of the factors away and use the larger number of pollard_rov for decomposition. If the complexity is involved, assume that the decomposition factor is p, and the time used is O (sqrt (p). In general, it is much faster than the direct sqrt (n) decomposition.

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <ctime>#include <map>#define MAXN 10#define INF 1LL<<61#define C 16381using namespace std;typedef long long LL;LL gcd(LL a, LL b){    return b ? gcd(b, a % b) : a;}LL multi(LL a, LL b, LL m){     LL ans = 0;     while(b)     {         if(b & 1)         {             ans = (ans + a) % m;             b--;         }         b >>= 1;         a = (a + a) % m;     }     return ans;}LL quick_mod(LL a, LL b, LL m){     LL ans = 1;     a %= m;     while(b)     {         if(b & 1)         {             ans = multi(ans, a, m);             b--;         }         b >>= 1;         a = multi(a, a, m);     }     return ans;}bool Miller_Rabin(LL n){    if(n == 2) return true;    if(n < 2 || !(n & 1)) return false;    LL a, m = n - 1, x, y;    int k = 0;    while((m & 1) == 0)    {        k++;        m >>= 1;    }    for(int i = 0; i < MAXN; i++)    {        a = rand() % (n - 1) + 1;        x = quick_mod(a, m, n);        for(int j = 0; j < k; j++)        {            y = multi(x, x, n);            if(y == 1 && x != 1 && x != n - 1) return false;            x = y;        }        if(y != 1) return false;    }    return true;}LL Pollard_rho(LL n, LL c){     LL x, y, d, i = 1, k = 2;     y = x = rand() % (n - 1) + 1;     while(true)     {         i++;         x = (multi(x, x, n) + c) % n;         d = gcd((y - x + n) % n, n);         if(1 < d && d < n) return d;         if(y == x) return n;         if(i == k)         {             y = x;             k <<= 1;         }     }}LL fac[555];int cnt;map<LL, int>mp;void find(LL n, int c){     if(n == 1) return;     if(Miller_Rabin(n))     {         mp[n]++;         return ;     }     LL p = n;     LL k = c;     while(p >= n) p = Pollard_rho(p, c--);     find(p, k);     find(n / p,k);}int main(){    LL a, b;    while(scanf("%I64d%I64d", &a, &b) != EOF)    {        cnt = 0;        LL tmp = b / a;        if(a == b)        {            printf("%I64d %I64d\n", a, b);            continue;        }        mp.clear();        find(tmp, C);        cnt = 0;        for(map<LL, int>:: iterator it = mp.begin(); it != mp.end(); it++)        {            LL t = 1;            for(int j = 0; j < it -> second; j++)                t = t * (it -> first);            fac[cnt++] = t;        }        //for(int i = 0; i < cnt; i++)           // printf("%I64d\n", fac[i]);        LL ansa = INF, ansb = INF;        for(int i = 0; i < (1 << cnt); i++)        {            LL ta = 1;            for(int j = 0; j < cnt; j++)                if(i & (1 << j))                    ta *= fac[j];            if(ansa + ansb > ta + b / a / ta)                ansa = ta, ansb = b / a / ta;        }        if(ansa > ansb) swap(ansa, ansb);        printf("%I64d %I64d\n", ansa * a, ansb * a);    }return 0;}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.