Cod sursa(job #366667)

Utilizator RegeleUmbrelorPopescu Mihai RegeleUmbrelor Data 22 noiembrie 2009 10:14:21
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include<stdio.h>
using namespace std;
long long p,q,b, v[20][2],nrp;

void citire()
{
	freopen("gfact.in", "r", stdin);
	freopen("gfact.out","w", stdout);
	scanf("%lld %lld", &p, &q);
	long long d=2, i=0,x=p;
	while(x!=1&&d*d<=p)
	{
		if(x%d)
			d++;
		else
		{	
			v[++i][0]=d;
			while(!(x%d))
			{
				v[i][1]++;
				x/=d;
			}
			v[i][1]*=q;
		}
	}
	if(x!=1 && d*d>p)
	{	
		v[++i][0]=x;
		v[i][1]=q;
	}
	nrp=i;
}
long long cautare()
{
	long long i,j, max=0,x,s=0;
	for(i=1;i<=nrp;++i)
	{	
		s=0;j=0;
		while(s<v[i][1])
		{	
			j+=v[i][0];x=j;
			while(!(x%v[i][0]))
			{
				s++;x/=v[i][0];
			}
		}
		if(max<=j);
			max=j;
	}
	return max;
}
	
int main()
{
	citire();
	printf("%lld",cautare());
	return 0;
}