Cod sursa(job #1117729)

Utilizator iu.ciocoiuIulian iu.ciocoiu Data 23 februarie 2014 19:19:22
Problema GFact Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include<iostream>
using namespace std;

unsigned putere(unsigned a, unsigned b)
{
    unsigned rez=1;
    while(b>1)
    {   if(b%2) { rez*=a; b--; }
        a*=a; b/=2;
    }
    return rez*a;
}

int main()
{
    unsigned p,q,ex,aux,cop,d=2,max=0,exmax;
    cout<<"p="; cin>>p;
    cout<<"q="; cin>>q;
    while(p>1)
        {   ex=0;
            while(p%d==0)
                { p=p/d; ex++; }
            if(ex)
                {   //cout<<d<<"^"<<ex<<"\n";
                    aux=putere(d,ex*q);
                    if(aux>max) { max=d; exmax=ex*q; }
                    //cout<<"aux="<<aux<<endl;
                    //cout<<"ex*q="<<ex*q<<endl;
                }
            d++;
        }
    aux=max;
    //cout<<"max="<<max<<endl;
    //cout<<"exmax="<<exmax<<endl;
    d=0;
    while(exmax>0)
    {
        d++;
        cop=d;
        while(cop%max==0)
            {   exmax--;
                cop=cop/max;
            }
    }
    cout<<"B="<<d<<endl;
    return 0;
}