Cod sursa(job #806160)

Utilizator lupuletiLupuleti Catalin lupuleti Data 1 noiembrie 2012 22:18:06
Problema Factorial Scor 55
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
# include<fstream>
using namespace std;
int nr_zerouri(int n)
{
    long int p;
    p=0;
    while (n>0)
    {
        p=p+n/5;
        n=n/5;
    }
    return p;
}
int main()
{
    ifstream cin("fact.in");
    ofstream cout("fact.out");
    long int p,n,a,b,c;
    cin >> p;
    n=1; a=1; c=1;
   b=100000010;
    while(a<=b)
    {
        c=(a+b)/2;
        n=nr_zerouri(c);
      //  cout << n << " " << a << " " << b << endl;
        if(n==p)
        {
            a=c+1; b=c;
        }
        else
        {

            if(n<p)
            {
                a=c+1;
            }
            else
            {
                b=c-1;
            }
        }
    }
    if(nr_zerouri(b)!=p)
    {
        cout << -1 ;
    }
    else{

    while(nr_zerouri(b)==p)
    {
        b--;
    }
    cout << b+1;
    }
    /* while(x<p)
    {
      n++;
      x=nr_zerouri(n);
    }
    if(x==p)
    {
        cout << n;
    }
    else
    {
        cout << "-1" ;
    }
    */
}