Cod sursa(job #469253)

Utilizator hendrikHendrik Lai hendrik Data 7 iulie 2010 06:36:15
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

#define N 100000
typedef long long ll;
int p,q,k,v1[N],v2[N];

void open(){
	freopen("gfact.in","r",stdin);
	freopen("gfact.out","w",stdout);
}

void factorize(int p){
	k=0;
	for (int i=2;i*i<=p;i++){
		int m=0;
		while (p%i==0){
			m++;
			p/=i;
		}
		if (m){
			v1[k]=i;
			v2[k]=m;
			k++;
		}
	}
	if (p!=1){
		v1[k]=p;
		v2[k]=1;
		k++;
	}
}

ll cnt(ll x,ll y){
	ll res=0;
	while (x>=y){
		res+=(ll)(x/=y);
	}
	return res;
}

bool ok(ll x){
	for (int i=0;i<k;i++){
		if (cnt(x,v1[i])<(ll)(q)*v2[i]){
			return 0;
		}
	}
	return 1;
}

ll search(int p,int q){
	factorize(p);
	ll lo,hi,mid,res;
	lo=0;hi=(1LL<<60);
	while (1){
		mid=(lo+hi)>>1;
		if (ok(mid)){
			res=mid;
			hi=mid-1;
		}
		else lo=mid+1;
		if (lo>hi) break;
	}
	return res;
}

int main(){
	open();
	scanf("%d%d",&p,&q);
	printf("%lld\n",search(p,q));
	//system("pause");
	return 0;
}