Cod sursa(job #1376282)

Utilizator thaghostAndrei Dulceanu thaghost Data 5 martie 2015 16:53:11
Problema Factorial Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 1.01 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) {
			long lo = 1;
			long hi = (long) (Math.pow(5, 9) * Math.pow(2, 9));
			long m = -1;

			while (lo <= hi) {
				m = (lo + hi) / 2;
				long 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 long howManyZeros(long n) {
		long res = 0;
		for (int i = 1; i <= 20; i++) {
			res += n / (long) Math.pow(5, i);
		}

		return res;
	}
}