Cod sursa(job #1833717)

Utilizator sabinantonSabin Anton sabinanton Data 22 decembrie 2016 22:43:18
Problema GFact Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream f("fact.in");
    ofstream g("fact.out");
    long long st=1,dr=1000000000,mid,ct,p,b,fi=-1;

   f>>b>>p;
    while(st<=dr)
    {
        mid=(st+dr)/2;
        ct=0;
        for(int i=b;i<=mid;i=i*b)
            ct+=(mid/i);
        if(p==ct)
        {
            fi=mid;
            dr=mid-1;
        }
        else
        {
            if(ct<p)
            {
                st=mid+1;
            }
            else
                dr=mid-1;
        }
    }
    g<<fi;
    return 0;
}