Cod sursa(job #359314)

Utilizator Anamaria20Cotirlea Anamaria Anamaria20 Data 26 octombrie 2009 16:30:33
Problema GFact Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <stdio.h>

FILE *f,*s;

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

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

void cautBin()
{
	int st=1;
	int dr=2000000;
	
	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)
			break;
		
		if(cat<v[i].y)
			st=mj+1;
		
		if(cat>v[i].y)
			dr=mj-1;
	}

	if(st>rez)
		rez=st;
}	

int main()
{
	f=fopen("gfact.in","r");
	s=fopen("gfact.out","w");
	
	fscanf(f,"%d %d",&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,"%d",rez);
	
	fclose(s);
	
	return 0;
}