Cod sursa(job #222090)

Utilizator mISHOOOmISHOOO mISHOOO Data 19 noiembrie 2008 23:46:59
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <stdio.h>

FILE *fi = fopen("fact.in", "r");
FILE *fo = fopen("fact.out", "w");

unsigned long P, N, z, st, dr, ct;
unsigned long n5(unsigned long N) {
	unsigned  long N5 = N/5, P=5, j, R;

	while (P<=N/5) {
		P = P*5;
		N5+=N/P;
	}

	return N5;
}

int main() {
	fscanf(fi, "%d", &P);

	st = 0; dr = 4294967295;
	if (P==0) fprintf(fo, "1\n");
	else
		if (n5(4294967295)==P) fprintf(fo, "4294967295\n");
		else
			while (st<dr) {
				if (dr==st+1) {
					z = n5(st);
					if (z==P) fprintf(fo, "%ld\n", st);
					else {
						z = n5(dr);
						if (z==P) fprintf(fo, "%ld\n", dr);
						else fprintf(fo, "-1\n");
					}
					break;
				}
				else {
					ct = (st+dr)/2;
					z = n5(ct);
					if (z<P) st = ct; else dr = ct;
				}
			}

	
	fclose(fi);
	fclose(fo);

	return 0;
}