Cod sursa(job #359332)

Utilizator Anamaria20Cotirlea Anamaria Anamaria20 Data 26 octombrie 2009 17:44:56
Problema GFact Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <stdio.h>

FILE *f,*s;

long long int p,q,l,i,j,x,y,cat,rez;

struct putere
{
	long long int x;
	long long int y;
};	
	
putere v[2000];

void cautBin()
{
	long long int st=1;
	long long int dr=2000000000;
	
	long long int mj;
	
	while(st<dr)
	{
		mj=(st+dr)/2;
		
		cat=0;
		
		y=v[i].x;
		while(y<=mj)
		{
			cat+=mj/y;
			y*=v[i].x;
		}	
		
		if(cat<v[i].y)
			st=mj+1;
		
		if(cat>=v[i].y)
			dr=mj;
	}

	if(st>rez)
		rez=st;
	
}	

int main()
{
	f=fopen("gfact.in","r");
	s=fopen("gfact.out","w");
	
	fscanf(f,"%lld %lld",&p,&q);
	
	x=0;
	while(p%2==0)
	{
		x++;
		p/=2;
	}	
	
	if(x>0)
	{
		l++;
		
		v[l].x=2;
		v[l].y=x;
	}	
	
	for(i=3;i*i<=p;i+=2)
	{
		x=0;
		while(p%i==0)
		{
			x++;
			p/=i;
		}	
	
		if(x>0)
		{
			l++;
		
			v[l].x=i;
			v[l].y=x;
		}
	}	
	
	if(p>1)
	{
		l++;
		
		v[l].x=p;
		v[l].y=1;
	}	
	
	for(i=1;i<=l;i++)
		v[i].y*=q;
		
	for(i=1;i<=l;i++)
		cautBin();
	
	fprintf(s,"%lld",rez);
	
	fclose(s);
	
	return 0;
}