Cod sursa(job #1500479)

Utilizator hasmasandragosHasmasan Dragos hasmasandragos Data 11 octombrie 2015 23:39:56
Problema GFact Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("gfact.in");
ofstream g("gfact.out");

const int nmax=5000005;
long long int i,p,q,divizor,put,factmax,nrprim,boss,added;
long long int primes[nmax],fr[nmax];

bool prim()
{
    long long int h;
    for (h=2;h*h<=p;h++)
     if (p%h==0)
      return 0;
    return 1;
}

bool descompunere (long long int numar , long long int necesar ,long long int ori)
{
    long long int imp,powsofar,current=1;
    powsofar=0;
    while (powsofar<necesar && current<=numar/ori)
    {
        current*=ori;
        powsofar+=(numar/current);
    }

    if (powsofar>=necesar)
     return 1;
   return 0;
}

long long int bin_search (long long int frx , long long x)
{
    long long int st,dr,numar,mij,lastp=-1,aux;
    st=0;
    dr=(1<<30);
    dr*=dr;

    while (st<=dr)
    {
        mij=(st+dr)/2;
        numar=mij;
        aux=descompunere(numar,frx,x);
        if (aux)
        {
            lastp=numar;
            dr=mij-1;
        }
        else
           st=mij+1;
    }
    if (lastp>0) return lastp;
    return -1;
}

int main()
{
    f>>p>>q;
    divizor=2;

    if (p==1)
    {
        g<<1;
        return 0;
    }

    if (prim())
    {
        boss=bin_search(q,p);
        if (boss>factmax)
             factmax=boss;
        added=1;
    }

    while (p>1 && added==0)
    {
        put=0;
        while (p%divizor==0)
        {
            p/=divizor;
            put++;
        }
        if (put>0)
        {
            boss=bin_search(put*q,divizor);
            if (boss>factmax)
             factmax=boss;
        }
         divizor++;
    }

    g<<factmax;
    return 0;
}