Cod sursa(job #1804261)

Utilizator mateilmatei lascu mateil Data 12 noiembrie 2016 13:25:38
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include<iostream>
using namespace std;
int v[20],y[20],nd;
void prim(int n)
{
    int d = 2;
    nd = 0;
    while(d * d <= n)
    {
        if(n % d == 0)
        {
            v[nd] = d;
            while(n % d == 0)
            {
                y[nd]++;
                n = n / d;
            }
            nd++;
        }
        d++;
    }
    if(n != 1)
    {
        v[nd] = n;
        y[nd] = 1;
        nd++;
    }
    return;
}
long long putere(int pr, long long n){
    long long x = 0;
    while(n > 0){
     x +=n / pr;
     n = n / pr;
    }
    return x;
}
int div(long long n){
 int i;
 for(i = 0; i < nd; i++){
     if(putere(v[i],n) < y[i])
      return 0;
 }
 return 1;
}
int main()
{
    ifstream f("gfact.in");
    ofstream g("gfact.out");
    int i,p,q;
    long long s = 0,pas;
    f>>p>>q;
    prim(p);
    for(i = 0; i < nd; i++)
    {
        y[i]*=q;
    }
    pas = 1LL<<60;
    while(pas > 0){
     if(div(s + pas) == 0)
      s+=pas;
     pas = pas/2;
    }
    g<<s + 1;
    return 0;
}