Cod sursa(job #1376211)

Utilizator thaghostAndrei Dulceanu thaghost Data 5 martie 2015 16:34:32
Problema Factorial Scor 20
Compilator java Status done
Runda Arhiva de probleme Marime 1 kb
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(new FileInputStream("fact.in"));
		PrintWriter out = new PrintWriter("fact.out");

		int n = in.nextInt();
		in.close();

		if (n != 0) {
			int lo = 1;
			int hi = (int) (Math.pow(5, 8) * Math.pow(2, 8));
			int m = -1;

			while (lo <= hi) {
				m = (lo + hi) / 2;
				int z = howManyZeros(m);

				if (z == n) {
					break;
				}

				if (z < n) {
					lo = m + 1;
				} else {
					hi = m - 1;
				}
			}

			if (howManyZeros(m) == n) {
				while (m % 5 != 0) {
					m--;
				}
				out.println(m);
			} else {
				out.println(-1);
			}
		} else {
			out.println(1);
		}
		out.close();
	}

	public static int howManyZeros(int n) {
		int res = 0;
		for (int i = 1; i <= 8; i++) {
			res += n / (int) Math.pow(5, i);
		}

		return res;
	}
}