Cod sursa(job #1541339)

Utilizator sulzandreiandrei sulzandrei Data 3 decembrie 2015 22:26:53
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("factorial.in");
ofstream out("factorial.out");
int fives[100000000],twos[100000000];
int main()
{
    int n,lo,hi,p;
    in >> p;
    twos[ 0 ]  = fives[ 0 ] = 1;
    for(int i = 1 ; i <= 1000000 ; i ++)
    {
        if (i % 5 == 0)
            fives[i] = fives[i-1]+1;
        else
            fives[i] = fives[i-1];
        if( i % 2 == 0)
            twos[i ] = twos[ i-1] +1;
        else
            twos[ i ] = twos[ i-1];
    }
    cout<<fives[45];
    lo = 1; hi = 1000000;
    while ( lo < hi)
    {
        int mij = (lo + hi ) /2;
        if(  p <= min(fives[mij],twos[mij]))
            hi = mij;
        else
            lo = mij+1;
    }
    if( min(fives[lo],twos[lo]) == p)
        out<<lo;
    else
        out<<"-1";



    return 0;
}