Cod sursa(job #1536086)

Utilizator LucianTLucian Trepteanu LucianT Data 25 noiembrie 2015 17:50:33
Problema GFact Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <cstdio>
#include <algorithm>
#include <climits>
#define maxN 200005
using namespace std;
int p, q, i, sol, st=0, dr=INT_MAX, m;
struct nr
{
    int pt, e;
}v[maxN];
bool ok;
//calculez exp din n!
int legendre(int n, int f)
{
    int p=f, s=0;
    while(p <= n)
        s += n/p, p *= f;
    return s;
}
int main()
{
    freopen("gfact.in", "r", stdin);
    freopen("gfact.out", "w", stdout);
    scanf("%d %d", &p, &q);
    int d=2, k=0;
    //caz particular
    if(p == 1)
    {
        printf("1");
        return 0;
    }
    //descompun
    while(p > 1)
    {
        if(p%d == 0)
            v[++k].pt=d;
        while(p%d == 0)
            v[k].e++, p/=d;
        if(d * d > p && p > 1)
            v[++k].pt=p,v[k].e=1,p=1;
        d++;
    }
    //caut binar solutia
    while(st <= dr)
    {
        m = (st+dr)/2;
        ok=true;
        for(i = 1; i <= k; i++)
            if((legendre(m, v[k].pt) < v[k].e * q))
                ok=false;
        if(ok) sol=m,dr=m-1;
        else st=m+1;
    }
    printf("%d",sol);
    return 0;
}