Cod sursa(job #1399271)

Utilizator DysKodeTurturica Razvan DysKode Data 24 martie 2015 17:37:52
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>

using namespace std;

ifstream fin("fact.in");
ofstream fout("fact.out");

int src,Left,Right,Middle,nrCurZero;

int _NrZero(int x)
{
    int s = 0;
    while( x != 0 )
    {
        s += x / 5;
        x /= 5;
    }

    return s;
}


int main()
{
    fin>>src;

    if( src == 0 )
    {
        fout<<1;
        return 0;
    }

    Left = 1;
    Right = 2000000000;
    while( Left <= Right )
    {
        Middle = ( Left + Right ) / 2;

        nrCurZero = _NrZero( Middle );

        if( nrCurZero == src )
        {
            if( Middle % 5 == 0 )
            {
                fout<<Middle;
                return 0;
            }
            Right = Middle - 1;
        }

        if( nrCurZero > src )
            Right = Middle - 1;
        else if( nrCurZero < src )
            Left = Middle + 1;
    }

    fout<<-1;

return 0;
}