Cod sursa(job #1593215)

Utilizator mirupetPetcan Miruna mirupet Data 8 februarie 2016 13:41:08
Problema GFact Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

int k, put, i = 3;
long long  P, Q, MAX;
long long bs(int, int);

int main()
    {
        freopen("gfact.in","r",stdin);
        freopen("gfact.out","w",stdout);

        scanf("%d%d", &P, &Q);

        if (P % 2 == 0)
        {
            put = 0;
            while (P % 2 == 0)
            {
                put++;
                P /= 2;
            }

            MAX = max(MAX, bs(2, put * Q));
        }

        while (1LL * i * i <= P)
        {
            if (P % i == 0)
            {
                put = 0;
                while (P % i == 0)
                {
                    put ++;
                    P /= i;
                }

                MAX = max(MAX, bs(i, put * Q));
            }
            i += 2;
        }

        if (P > 1)
            MAX = max(MAX, bs(P, Q));

        printf("%lld", MAX);
    }

long long bs(int A, int B)
{
    long long left = 0, right = 1LL * A * B;

    while (left <= right)
    {
        long long ans = (left + right) / 2;
        int sol = 0, C = A;
        while (C <= ans)
        {
            sol += ans / C;
            C *= A;
        }

        if (sol < B)
            left = ans + 1;
        else
            right = ans - 1;
    }

    return left;
}