Cod sursa(job #67090)

Utilizator gigi_becaliGigi Becali gigi_becali Data 22 iunie 2007 15:40:53
Problema GFact Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include<fstream.h>
#include<stdio.h>
#include<math.h>

long long unsigned p;
long long unsigned q, b, nr;
long long unsigned a, v[10], ex;

void citire()
{
  ifstream in("gfact.in");
  in>>p>>q;
  in.close();
}

int prim(int x)
{
  if (x==2) return 1;
  if (x==1) return 0;
  if (x%2==0) return 0;
  for (int d=3; d*d<=x; d+=2)
    if (x%d==0) return 0;
  return 1;
}

void descomp()
{
  int ok=1;b=p;
  for (int  d=2; d<=p; d++)
    {
      if (p%d==0)
      {
	v[++nr]=d;
	ex=0;
	while (p%d==0) { p/=d; ex++;}
	if (prim(p)) { v[++nr]=p; ex=1; ok=0;}
      }
      if (ok==0) {p=v[nr]; break;}
    }    
}


void aflu()
{
  int i, contor=0;
  if (p==1)
  {
  i=v[nr];
  while (contor<q*ex)
   {
     int  j;
     j=i;
     if (j%v[nr]==0)
       while (j%v[nr]==0)
	 {
	   contor++;
	   j/=v[nr];
	 }
     i+=v[nr];
   }
 
  printf("%llu",i-v[nr]);
  }
  else 
  {//freopen("gfact.out","w",stdout);
  printf("%llu",v[nr]*q);}
}

int main()
{
  citire();
 freopen("gfact.out","w",stdout); 
  descomp();
  aflu();
  return 0;
}