Cod sursa(job #650538)

Utilizator SteveStefan Eniceicu Steve Data 18 decembrie 2011 13:36:58
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <fstream>
#include <iostream>
#include <math.h>

using namespace std;

long long P, Q, i, radacina, x[100], y[100], nr = -1;
long long rezultat, chestie;
long long c;
long long aux;

void Citire ()
{
    ifstream fin ("gfact.in");
    fin >> P >> Q;
    fin.close ();
}

void Initializare ()
{
    rezultat = (P * Q);
    radacina = (long long) sqrt (P);
    for (i = 2; i <= radacina; i++)
    {
        if (P % i == 0)
        {
            x[++nr] = i;
            y[nr] = 0;
            while (P % i == 0)
            {
                y[nr]++;
                P /= i;
            }
        }
        if (P == 1) break;
    }
    if (P != 1)
    {
        x[++nr] = P;
        y[nr] = 1;
    }
}

int Grunt_Work (long long a)
{
    if (a < 0) return 0;
    for (i = 0; i <= nr; i++)
    {
        c = 0;
        aux = a;
        while (aux)
        {
            c += (aux / x[i]);
            aux /= x[i];
        }
        if (c < y[i] * Q) return 0;
    }
    return 1;
}

long long Cautare_Binara ()
{
    long long step, j;
    for (step = 1; step < rezultat; step <<= 1);
    for (j = rezultat; step; step >>= 1)
    {
        if (Grunt_Work (j - step)) j -= step;
    }
    return j;
}

void Scriere ()
{
    ofstream fout ("gfact.out");
    rezultat = Cautare_Binara ();
    fout << rezultat;
    fout.close ();
}

int main ()
{
    Citire ();
    Initializare ();
    Scriere ();
    return 0;
}