Cod sursa(job #1126314)

Utilizator CostanMiriamCostan Miriam CostanMiriam Data 26 februarie 2014 22:36:19
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>

using namespace std;

ifstream fin ("gfact.in");
ofstream fout ("gfact.out");

long long p,q,i,putere,ok,x,st,dr,s,sol,mij;

int main () {

    fin>>p>>q;

    if (p==1){
        fout<<1<<"\n";
        return 0;
    }

    for (i=2;i*i<=p;i++) {

        putere=ok=0;

        while (p%i==0) {
            ok=1;
            putere++;
            p/=i;
        }

        if ( ok==1 ) {

            st=1;dr=40000000000001;

            while (st<=dr) {

                s=0;
                mij=(st+dr)/2;
                x=i;

                while (mij/x!=0) {
                    s+=mij/x;
                    x*=i;
                }

                if (s<putere*q)
                    st=mij+1;
                else
                    dr=mij-1;
            }

            if (sol<st)
                sol=st;
        }

    }

    if (p!=1) {
        st=1;dr=40000000000001;

        while (st<=dr) {

            s=0;
            mij=(st+dr)/2;
            x=p;

            while (mij/x!=0) {
                s+=mij/x;
                x*=p;
            }

            if (s<q)
                st=mij+1;
            else
                dr=mij-1;
        }

        if (sol<st)
            sol=st;
    }

    fout<<sol<<"\n";

    return 0;
}