Cod sursa(job #2085143)

Utilizator nicolaefilatNicolae Filat nicolaefilat Data 9 decembrie 2017 19:10:14
Problema GFact Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <iostream>
#include <fstream>
#include <vector>

typedef unsigned long long int u;

using namespace std;

ifstream in("gfact.in");
ofstream out("gfact.out");

int p,q,puteri[10000],exponent[10000];
int index1,index2;

bool validare(u x){

    bool ok = false;

    for(int i = 0; i < index1; i ++){

        u a = puteri[i];
        int p = exponent[i];
        u cnt = 0;

        while(x >= a){
            cnt += x / a;
            a *= puteri[i];
        }
        if(p <= cnt)
            ok = true;
    }
    return ok;
}

int main()
{
    in>>p>>q;


    //divizori
    int d = 2,cnt;
    while(d*d <= p){
        cnt = 0;
        if(p % d == 0){
            while(p % d == 0){
                p /= d;
                cnt++;
            }
            puteri[index1++] = d;
            exponent[index2++] = cnt*q;
        }
        d++;
    }
    if(p != 1){
        puteri[index1++] = p;
        exponent[index2++] = q;
    }

    //cautare binara
    u pas = 1LL<<45,r = 0;

    while(pas){
        if(validare(r+pas) == false)
            r += pas;
        pas >>=1;
    }
    out<<r+1;


    return 0;
}