Cod sursa(job #2078549)

Utilizator CryshanaGanea Carina Cryshana Data 29 noiembrie 2017 18:35:00
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

const int M=100000;
pair<long long,long long> ds[M];
long long nd;

bool divid (long long x)
{
    long long i,p;
    long long j;
    for(i=1; i<=nd ; i++)
    {
        p=0;
        j=ds[i].first;
        while(j<=x)
        {
            p+=(x/j);
            j*=ds[i].first;
        }
        if(p<ds[i].second) return 0;
    }
    return 1;
}


int main()
{
    ifstream fin("gfact.in") ;
    ofstream fout("gfact.out");
    long long p,q,d,x,r=0;
    long long pas=1LL<<45;
    fin>>p>>q;
    d=2;
    x=p;
    while(d*d<=x&&p!=1)
    {
        if(p%d==0)
        {
            ++nd;
            ds[nd].first=d;
            while(p%d==0)
            {
                ds[nd].second++;
                p/=d;
            }
            ds[nd].second*=q;
        }
        d++;
    }
    if(p!=1)
    {
        ++nd;
        ds[nd].first=p;
        ds[nd].second=q;
    }
    while(pas!=0)
    {
        if(!divid(r+pas))
            r+=pas;
        pas/=2;
    }
    if(divid(r))
        fout<<r;
    else fout<<r+1;
    return 0;
}