Cod sursa(job #3226534)

Utilizator Andrei_RusuAndrei Alexandru Rusu Andrei_Rusu Data 21 aprilie 2024 19:22:54
Problema GFact Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("gfact.in");
ofstream fout("gfact.out");
int p, q, d ,e;
long long fast_expo(int b, int expo)
{
    long long a=b, p=1;
    for(;expo;expo>>=1)
    {
        if(expo&1) p*=a;
        a*=a;
    }
    return p;
}
long long legendre(long long n)
{
    long long numitor=d, s=0;
    while(numitor<=n)
    {
        s+=n/numitor;
        numitor*=d;
    }
    return s;
}
long long cautare_binare()
{
    long long st=1, dr=d*q*e, med, last=0;
    while(st<=dr)
    {
        med=(st+dr)/2;
        if(legendre(med)>=q)
        {
            last=med;
            dr=med-1;
        }
        else
            st=med+1;
    }
    return last;
}
void max_fact_prim()
{
    int n=p, dd=2, ee;
    d=e=1;
    while(n>1)
    {
        ee=0;
        while(n%dd==0)
        {
            ee++;
            n/=dd;
        }
        if(ee>0)
        {
            d=dd;
            e=ee;
        }
        dd++;
        if(dd*dd>=n)
            dd=n;
    }
}
int main()
{
    fin >> p >> q;
    max_fact_prim();
    fout << cautare_binare();
    return 0;
}