Cod sursa(job #1186142)

Utilizator piticutzu15Covaciu Andrei piticutzu15 Data 17 mai 2014 12:07:45
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <iostream>
#include <fstream>
using namespace std;
int fact(int n)
{
    int x = 5,rez=0;
    while(x <= n)
    {
        rez+=n/x;
        x *=5;
    }
    return rez;
}
int main()
{
    ifstream f("fact.in");
    ofstream g("fact.out");
    int n,p;
    f>>p;
    if(p == 0)
    {
        g<<"1";
        return 0;
    }
    int st=1,dr=(1LL<<30),ok=0,m;
    while(st < dr)
    {
            m = (st + dr) / 2;
            if(p > fact(m))
                st = m + 1;
            if(p < fact(m))
                dr = m - 1;
            if(p==fact(m))
            {
                ok=1;
                break;
            }
    }
    while(fact(m)>=p&&m>=0)
    {
        if(fact(m)>=p)
            --m;
    }
    if(ok)
        g<<m+1;
    else
        g<<"-1";
    return 0;
}