Cod sursa(job #2099744)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 4 ianuarie 2018 17:20:18
Problema GFact Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb

#include <fstream>
using namespace std;
ifstream f("gfact.in");
ofstream g("gfact.out");
int fact[200],exp[200],n,q;
int zero(long long n,long long m)
{
    int nr=0;
    while(n>=m) {
        nr+=(n/m);
        n=n/m;
    }
    return nr;
}
bool verif(int nr)
{
    for(int i=0;i<n;i++)
        if(zero(nr,fact[i])<exp[i]*q)
            return 0;
    return 1;
}
int main()
{
    long long p,i,r=0,step=1<<30;
    f>>p>>q;
    if(p%2==0)
    {
        fact[0]=2;
        while(p%2==0)
        {
            p/=2;
            exp[0]++;
        }
        n=1;
    }
    if(p%3==0)
    {
        fact[n]=3;
        while(p%3==0)
        {
            p/=3;
            exp[n]++;
        }
        n++;
    }
    for(i=5;p>1&&i*i<=p;i+=6)
    {
        if(p%i==0)
        {
            fact[n]=i;
            while(p%i==0)
            {
                p/=i;
                exp[n]++;
            }
            n++;
        }
        if(p%(i+2)==0)
        {
            fact[n]=i+2;
            while(p%(i+2)==0)
            {
                p/=i+2;
                exp[n]++;
            }
            n++;
        }
    }
    if(p>1)
    {
        fact[n]=p;
        exp[n]=1;
        n++;
    }
    while(step)
    {
        if(!verif(r+step))
            r+=step;
        step>>=1;
    }
    g<<r+1;
    return 0;
}