Cod sursa(job #1734260)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 26 iulie 2016 21:47:36
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <fstream>
#include <math.h>
#include <algorithm>
#include <unordered_map>

using namespace std;
#define ll long long
#define llu long long unsigned
#define pb push_back
#define mp make_pair

string problemName = "gfact";
string inFile = problemName+".in";
string outFile = problemName+".out";
ifstream fin(inFile.c_str());
ofstream fout(outFile.c_str());

const long long MAX = 1e17;
unordered_map <int, int> need;

bool check(ll x, int y, int target){
    ll i;
    ll cnt = 0;
    for(i = y;i <= x;i *= y){
        cnt += x/i;
        if(cnt >= target){
            return true;
        }
    }
    return false;
}

ll binarySearch(ll st, ll dr, int x, int target){
    ll mid = (st+dr)/2;
    ll ret = 0;
    while(st <= dr){
        if(check(mid*x, x, target)){
            ret = mid;
            dr = mid-1;
        }else{
            st = mid+1;
        }
        mid = (st+dr)/2;
    }
    return ret*x;
}

void desc(int p, int q){
    int i,aux,c;
    c = 0;
    while(p%2 == 0){
        c++;
        p /= 2;
    }
    if(c){
        need[2] = c*q;
    }
    for(i = 3;i*i <= p;i += 2){
        if(p%i == 0){
            c = 0;
            while(p%i == 0){
                c++;
                p /= i;
            }
            need[i] = c*q;
        }
    }
    if(p > 1){
        need[p] = q;
    }
}

int main(){
    int p,q,i,aux,c,target,x;
    fin>>p>>q;
    desc(p, q);
    ll mn = 0;
    for(auto it : need){
        mn = max(mn, binarySearch(1LL, MAX/it.first, it.first, it.second));
    }
    fout<<mn;
    return 0;
}