"Pollard-rho algorithm" "DFS" poj2429 GCD & LCM Inverse

Source: Internet
Author: User
Tags gcd

Test instructions: give you one or two numbers m and N, each of which is a logarithmic a, a, B, gcd and LCM, allowing you to find a pair that makes a+b the smallest of a, B.

Of all the quality factors of n/m, some of them are only in a, and the other part is only in B.

So after decomposing the n/m factor, what is the quality factor of the DFS enumeration in a, what is in B, and then try to update the answer. (because the same quality factor can only be in a or B, and a long long has a different number of qualitative factors than 14)

Pay attention to the n==m of the special sentence.

#include <algorithm> #include <cstdio> #include <cstdlib> #define N 5500using 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) {retur    n 0;    } ll Ans=quick_mul (A,p>>1,mod);    Ans= (Ans+ans)%mod;    if ((P&AMP;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&AMP;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&AMP;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 algorithm "DFS" poj2429 GCD & LCM Inverse

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.