Cod sursa(job #2430143)

Utilizator andrei885Andrei Paul Vintila andrei885 Data 12 iunie 2019 21:16:32
Problema Factorial Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <stdio.h>

int check(int n, int p) {
  int temp = n, count = 0, f = 5;
  while (f <= temp) {
    count += temp / f;
    f = f * 5;
  }

  if (count == p)
    return 0;
  else if (count < p)
    return -1;
  else if (count > p)
    return 1;
}


void main() {
  int P;
  FILE *fp = fopen("fact.in", "r");
  fscanf(fp, "%d", &P);
  fclose(fp);
  
  int N = -1;
  if (!P)
    N = 1;
  else {
    int mid;
    int low = 0;
    int high = 5 * P;
    while (low <= high) {
      mid = (low + high) >> 1;

      if (check(mid, P) > 0)
        high = mid - 1;
      else if (check(mid, P) < 0)
        low = mid + 1;
      else {
        N = mid - mid % 5;
        break;
      }
    }
  }

  fp = fopen("fact.out", "w");
  fprintf(fp, "%d", N);
  fclose(fp);
}