Cod sursa(job #3209064)

Utilizator andreiciocanCiocan Andrei andreiciocan Data 1 martie 2024 20:09:32
Problema GFact Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("gfact.in");
ofstream fout("gfact.out");
int main()
{
    long long p,q,b;
    long long st,dt;
    fin>>p>>q;
    long long maxi=-1,maxip,divi,div=2;
    while(p>=div*div)
    {
        if(p%div==0)
        {
            int putere=0,x=1;
            while(p%div==0)
            {
                p=p/div;
                putere++;
                x=x*div;
            }
            if(x>maxi)
            {
                maxi=x;
                maxip=putere;
                divi=div;
            }
        }
        div++;
    }
    if(p!=1)
    {
        if(p>maxi){
            maxip=1;
            divi=p;
        }
    }
    q=q*maxip;
    p=divi;
    st=1;
    dt=p*q;
    while(st<=dt)
    {
        int m=(st+dt)/p/2;
        m=m*p;
        int s=0;
        for(long long i=p;i<=m;i*=p)
        {
            s=s+m/i;
        }
        if(s>=q)
        {
            dt=m-p;
            b=m;
        }
        else
        {
            st=m+p;
        }
    }
    fout<<b;
    return 0;
}