Cod sursa(job #2433402)

Utilizator dr_personalityEftime Andrei Horatiu dr_personality Data 27 iunie 2019 11:39:20
Problema GFact Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include<cmath>
using namespace std;
ifstream in("gfact.in");
ofstream out("gfact.out");

const int factmax = 10000;
int p, q, vfact[factmax], lfact, vpow[factmax];

int factor_a_in_bfactorial(int a, int b)
{
    int aux = a, ans = 0;
    while(aux<=b)
    {
        ans += b / aux;
        aux *= a;
    }

    return ans;
}

int check(int x)
{
    for(int i = 0; i<lfact; i++)
    {
        if(factor_a_in_bfactorial(vfact[i], x)<vpow[i])
        {
            return 0;
        }
    }

    return 1;
}

int bs(){
    int hi = 2000000000, lo = 0, mid;
    while(hi - lo>1)
    {
        mid = (hi + lo) / 2;

        if(check(mid)==1 && check(mid - 1)==0)
        {
            return mid;
        }

        if(check(mid)==1)
            hi = mid;
        if(check(mid)==0)
            lo = mid;
    }

    if(check(mid)==1 && check(mid - 1)==0)
    {
        return mid;
    }
    if(check(hi)==1 && check(hi - 1)==0)
    {
        return hi;
    }
    if(check(lo)==1 && check(lo - 1)==0)
    {
        return lo;
    }
}

int main()
{
    in>>p>>q;

    int lim = (int)sqrt(p), auxp = p;
    for(int i = 2; i<=lim; i++)
    {
        if(auxp%i==0)
        {
            vfact[lfact] = i;
            while(auxp%i==0)
            {
                vpow[lfact]++;
                auxp /= i;
            }

            lfact++;
        }
    }

    if(auxp!=0)
    {
        vfact[lfact] = auxp;
        vpow[lfact] = 1;
        lfact++;
    }

    for(int i = 0; i<lfact; i++)
    {
        vpow[i] *= q;
    }

    out<<bs()<<'\n';

    return 0;
}