Cod sursa(job #1700212)

Utilizator cristinamateiCristina Matei cristinamatei Data 9 mai 2016 20:29:20
Problema GFact Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <iostream>
#include <fstream>

using namespace std;

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

long long p;
int q, d[100000],e[100000], nd;

void divizori( long long x )
{
    int i;
    for( i = 2; i*i<= x; i++ )
        if ( x % i == 0 )
        {
            e[nd] = 0;
            while( x % i == 0 )
            {
                e[nd]++;
                x/=i;
            }
            d[nd] = i;
            nd++;
        }
    if ( x != 1 )
    {
        e[nd] = 1;
        d[nd] = x;
        nd++;
    }
}

int putere( int n, int nr )
{
    int s = 0;
    while( n >= nr )
    {
        s+=n/nr;
        n/=nr;
    }
    return s;
}

bool ok ( int n )
{
    int r;
    for ( int i = 0; i < nd; i++ )
    {
        r = putere(n, d[i]);
        if ( r < e[i]*q )
            return false;
    }
    return true;
}

long long cautare()
{
    long long pas = 1 << 30, i = 0;
    while( pas != 0 )
    {
        if ( !ok(i+pas) )
            i+=pas;
        pas/=2;
    }
    return i;
}

int main()
{
    in >> p >> q;
    divizori(p);
    out << cautare()+1;
    return 0;
}