Cod sursa(job #469371)

Utilizator pujnabDragos Gegea pujnab Data 7 iulie 2010 17:51:21
Problema GFact Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<stdio.h>
#include<math.h>
int ff[1001];
int pp[1001];




void descomp(unsigned long n)
{
	unsigned long i,j,e,d,lim;
	d=2;
	lim=sqrt(n);
	i=0;
	while(d<=lim&&n>1)
	{
		e=0;
		while(n%d==0)
		{
			++e;
			n=n/d;
		}
	if(e)
	{
		ff[++i]=d;
		pp[i]=e;
	}
	++d;
	}
	if(n>1)
	{
		ff[++i]=n;
		pp[i]=1;
	}
}




unsigned long mf(unsigned long f, unsigned long med)
{
	int m=0,i;
	descomp(f);
	for(i=1;ff[i]!=0;++i)
		m=m+med/ff[i]+1;
	return m;
}





unsigned long cautbin(unsigned long f,unsigned long e)
{
	unsigned long med,st=1,dr=(1<<31)-1,poz;
	while(st<=dr)
	{
		med=(st+dr)>>1;
		if(mf(f,med)>=e)
		{
			dr=med-1;
			poz=med;
		}
		else
			st=med+1;
	}
return poz;
}






int main()
{
	freopen("gfact.in","r",stdin);
	freopen("gfact.out","w",stdout);
	unsigned long i,p,q;
	scanf("%ld%ld",&p,&q);
	i=cautbin(p,q);
	printf("%ld ",i);
	return 0;
}