Cod sursa(job #808188)

Utilizator teoionescuIonescu Teodor teoionescu Data 6 noiembrie 2012 14:23:37
Problema GFact Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include<cmath>
#include<fstream>
using namespace std;
ifstream in("gfact.in");
ofstream out("gfact.out");
int prim(int x){
    int d;
    if((x==0)||(x==1)) return 0;
    if((x!=2)&&(x%2==0)) return 0;
    for(d=3;d*d<=x;d+=2) if(x%2==0) return 0;
    return 1;
}
int verif(long long n,long long a,long long q){
	int i=2,baz[100],exp[100],h=0,e=0,aux;
	bool sediv=true;
	while(a>1){
		if(a%i==0) if(prim(i)==1){
			while(a%i==0){
				a/=i;
				e++;
			}
			while((e>=exp[h])&&(h>0)) h--;
			h++;
			baz[h]=i;
			exp[h]=e;
			e=0;
		}
		i++;
	}
	for(i=1;i<=h;i++){
		aux=n;
		e=0;
		while(aux>=baz[i]){
			e+=aux/baz[i];
			aux /= baz[i];
		}
		if(e<(exp[i]*q)) sediv=false;
	}
	if(sediv==true) return 1;
	else return 0;
}

long long caut(long long p, long long q){
    int i=0, pas = 1 <<30;
    while(pas!=0){
        if(verif(i+pas,p,q)==0) i+=pas;
        pas >>=1;
		//cout<<pas<<" "<<i<<" "<<p<<" "<<q<<" "<<verif(i+pas,p,q)<<"\n";
    }
    return 1+i;
}
int main()
{
    long long p,q;
    in>>p>>q;
	out<<caut(p,q)<<"\n";
    return 0;
}