Cod sursa(job #2067527)

Utilizator mihai2003LLL LLL mihai2003 Data 16 noiembrie 2017 16:17:12
Problema GFact Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <map>
using namespace std;
map<int,int>puteri;
ifstream in("gfact.in");
ofstream out("gfact.out");
void descompunere(int n,int p){
    int d=2;
    while(d*d<=n){
        if(n%d==0){
            puteri[d]=0;
            while(n%d==0)
                n/=d,puteri[d]++;
        }
        d++;
    }
    if(n>1)
        puteri[n]=1;
    for(auto it=puteri.begin();it!=puteri.end();it++)
        (*it).second*=p;
}
int legendre(int n,int d){
  int rasp=0;
  while(d<=n){
    rasp+=(n/=d);
  }
  return rasp;
}
bool verif(int l){
    for(auto it=puteri.begin();it!=puteri.end();it++){
        if(legendre(l,(*it).first) >= (*it).second)
            return 1;

    }
    printf("%d\n",l);
    return 0;
}
int cbin(){
    int pas=1<<30,d=0;
    while(pas>0){
        if(!verif(pas+d))
            d+=pas;
        pas/=2;
    }
    return d+1;
}
int main()
{
    int p,q;
    in>>p>>q;
    descompunere(p,q);
    out<<cbin();
    return 0;
}