Cod sursa(job #3286880)

Utilizator lucky1992Ion Ion lucky1992 Data 14 martie 2025 19:20:30
Problema Factorial Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 1.04 kb
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {

  public static int countZeros(int v) {
    int count = 0;
    int p = 5;
    while (v / p != 0) {
      count += v / p;
      p *= 5;
    }

    return count;
  }

  public static int binarySearch(int low, int high, int P) {
    int res = -1;
    while (low <= high) {
      int mid = (low + high) >>> 1;

      int zeros = countZeros(mid);
      if (zeros == P) {
        res = mid;
        high = mid - 1;
      } else if (zeros < P) {
        low = mid + 1;
      } else {
        high = mid -1;
      }
    }

    return res;
  }


  public static void main(String[] args) throws FileNotFoundException {
    try (Scanner scanner = new Scanner(new FileInputStream("fact.in"));
         PrintStream pw = new PrintStream(new FileOutputStream("fact.out"))) {

      int P = scanner.nextInt();
      int res = binarySearch(1, Integer.MAX_VALUE, P);

      pw.println(res);
    }
  }
}