Cod sursa(job #1003245)

Utilizator poptibiPop Tiberiu poptibi Data 30 septembrie 2013 00:22:58
Problema GFact Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

long long P, Q;
long long Ans;
vector<pair<long long, long long> > F;

long long GetPow(long long Fact, long long Factorial)
{
    long long Sum = 0;
    for(long long i = 1LL * Fact; i <= Factorial; i *= Fact)
        Sum += Factorial / i;
    return Sum;
}

bool OK(long long Fact)
{
    for(vector<pair<long long, long long> > :: iterator it = F.begin(); it != F.end(); ++ it)
        if(GetPow(it -> first, Fact) < it -> second)
            return 0;
    return 1;
}

long long BS()
{
    long long Left = 1, Right = (1LL << 60), Mid, Now;
    while(Left <= Right)
    {
        Mid = Left + (Right - Left) / 2;
        if(OK(Mid)) Now = Mid, Right = Mid - 1;
        else Left = Mid + 1;
    }
    return Now;
}

int main()
{
    ifstream fin("gfact.in");
    ofstream fout("gfact.out");
    
    fin >> P >> Q;
    
    for(long long i = 2; 1LL * i * i <= P; ++ i)
        if(P % i == 0)
        {
            long long Exp = 0;
            while(P % i == 0) Exp ++, P /= i;
            F.push_back(make_pair(i, P * Q));
        }
    if(P > 1)
        F.push_back(make_pair(P, Q));
            
    fout << BS() << "\n";
    
    return 0;
}