Cod sursa(job #2797417)

Utilizator Cyf0Spineanu Rares Cyf0 Data 9 noiembrie 2021 20:53:56
Problema GFact Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
long long P, Q;
long legendre(long long n)
{
    long long x = P, nr = 0, y, exp, min = LONG_MAX;
    for (int i = 2; i * i <= x; i++)
    {
        exp = 0; 
        y = n;
        if (x % i == 0)
        {
            nr = 0;
            while (x % i == 0)
            {
                x /= i;
                exp++;
            }
            while (y >= i)
            {
                y /= i;
                nr += y;
            }
            nr /= exp;
            if (nr < min)
                min = nr;
        }
    }
    if (x >= 2)
    {
        nr = 0;
        y = n;
        while (y>=x)
        {
            y /= x;
            nr += y;
        }
        if (nr < min)
            min = nr;
    }
    return min;
}

long long binarysearch()
{
    long long P = 1, u = LONG_MAX, sol = 0;
    while (P <= u)
    {
        long long mij = (P + u) / 2;
        if (legendre(mij) < Q)
            P = mij + 1;
        else
        {
            u = mij - 1;
            sol = mij;
        }
    }
    return sol;
}

int main()
{
    ifstream fin("gfact.in");
    ofstream fout("gfact.out");
    fin >> P >> Q;
    fout << binarysearch();
    return 0;
}