Cod sursa(job #806871)

Utilizator ioana.teocIoana Teoc ioana.teoc Data 3 noiembrie 2012 17:50:16
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
//
//  main.cpp
//  Factorial
//
//  Created by Ioana Teoc on 11/3/12.
//  Copyright (c) 2012 Ioana Teoc. All rights reserved.
//

#include <iostream>
using namespace std;

int MAX = 500000000;

unsigned int numberOfZeros(int n){
    unsigned int no = 0;
    
    for(int p = 5; n/p != 0; p *= 5)
        no += n/p;
        
    return no;
}

int search(unsigned int p){
    int i, step;
    for(step = 1; step < MAX; step <<= 1);
    for(i = 0; step; step >>= 1){
        if(i + step <= MAX && (numberOfZeros(i + step) <= p))
            i += step;
    }
    
    if(numberOfZeros(i) != p)
        return -1;
    
    while(numberOfZeros(i-1) == p && i-1 >= 1)
        i--;
    return i;
}


int main()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    unsigned int p;
    scanf("%d",&p);
    int res = search(p);
    printf("%d", res);
}