Cod sursa(job #1693900)

Utilizator manciu_ionIon Manciu manciu_ion Data 24 aprilie 2016 10:10:44
Problema Factorial Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 1.01 kb
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;

public class MinFactorialNumberWithNZeros {

    static int getCountOfZeros(int x) {
        int count = 0;
        while (x > 0) {
            x /= 5;
            count += x;
        }
        return count;
    }

    static int binarySearch(int x) {
        int i = -1;
        for (int step = 1 << 30; step > 0; step >>= 1)
           if ( getCountOfZeros(i + step) < x) {
               i += step;
           }
        return ( i + 1 );
    }

    public static void main(String[] args) throws IOException{
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new FileReader("fact.in")));
        StreamTokenizer out = new StreamTokenizer(new BufferedReader(new FileReader("fact.out")));

        int n;
        in.nextToken(); n = (int) in.nval;
        int ans = binarySearch(n);
        System.out.println( (getCountOfZeros(ans) == n)? ans: -1 );

    }
}