Cod sursa(job #457783)

Utilizator veliki.velicuVelicu Stefan veliki.velicu Data 21 mai 2010 16:08:15
Problema GFact Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include<fstream>
using namespace std;

ifstream in("gfact.in");
ofstream out("gfact.out");

int q, d[20], p[20], n;

void divizor(int x)
{
	int i;
	for(i=2; i*i<=x; i++)
		if(x%i == 0)
		{
			d[++n]=i;
			while(x%i==0)
			{
				++p[n];
				x/=i;
			}
		}
	if(x!=1)
	{
		d[++n] = x;
		p[n]=1;
	}
}

int putere(int x, int d)
{
	int nr=0;
	while(x)
	{
		nr+=x/d;
		x/=d;
	}
	return nr;
}

bool factor(int x)
{
	int aux;
	for(int i=1; i<=n; i++)
	{
		aux=putere(x,d[i]);
		if(aux<p[i]*q)
			return false;
	}
	return true;
}

int binar()
{
	int i;
	int pos = 1<<30;
	for(i=0; pos!=0; pos/=2)
		if(!factor(i+pos))
			i+=pos;
	return 1+i;
}

int main()
{
	int p, n;
	in>>p>>q;
	divizor(p);
	n=binar();
	out<<n<<"\n";
	return 0;
}