Cod sursa(job #71822)

Utilizator cos_minBondane Cosmin cos_min Data 11 iulie 2007 19:00:08
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <stdio.h>
#include <fstream>
using namespace std;

#define in "fact.in"
#define out "fact.out"
#define llu long long unsigned 

llu P, N, St, Dr, Mij, Pivot;
llu T, X, Sol = 0;

int main()
{
    freopen(in,"r",stdin);
    freopen(out,"w",stdout);
    
    scanf("%llu", &P);
    
    St = 1, Dr = 10000000000LL;
    
    while ( St < Dr )
    {
          Mij = (St+Dr)>>1;
          Pivot = Mij;
          T = 0; X = 5;
          while ( Pivot >= X )
          {
                T += Pivot / X;
                X *= 5;
          }
          
          if ( T < P ) St = Mij + 1;
          else if ( T == P ) Sol = Mij, Dr = Mij-1;
          else               Dr = Mij-1;
    }
    
    if ( Sol == 0 ) 
    {
         printf("-1\n");
         return 0;
    }
    if ( Sol % 5 != 0 ) Sol -= 1;
    printf("%llu", Sol);
}