Cod sursa(job #1118012)

Utilizator iu.ciocoiuIulian iu.ciocoiu Data 23 februarie 2014 22:16:32
Problema GFact Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include<iostream>
#include<fstream>
#include<math.h>
using namespace std;

unsigned putere(unsigned a, unsigned b)
{   unsigned rez=1;
    while(b)
    {   if(b&1) { rez*=a; b--; }
        a*=a; b>>=1;   }
    return rez;
}
int main()
{   unsigned long long p,q,ex,aux,cop,d=2,max=0,exmax;
    ifstream f("gfact.in");
    ofstream g("gfact.out");
    f>>p>>q;
    cop=p;
    while((p>1)&&(d<=sqrt(p)))
        {   ex=0;
            while(p%d==0)
                { p=p/d; ex++; }
            if(ex)
                {   aux=putere(d,ex);
                    //cout<<d<<"^"<<ex<<endl;
                    if(aux>max) { max=d; exmax=ex*q; }
                }
            d++;
        }
    p=cop;
    //cout<<"max="<<max<<"exmax="<<exmax<<endl;
    if(d>sqrt(p)) { max=p; exmax=q; }
    //cout<<"max="<<max<<"exmax="<<exmax<<endl;
    d=0;
    while(exmax>0)
    {   d=d+max; cop=d;
        while((cop%max==0)&&(exmax>0))
            {   exmax--;
                cop=cop/max;
            }
    }
    g<<d;
    return 0;
}