Cod sursa(job #599841)

Utilizator Sanduleac_VladSanduleac Vllad Alexandru Sanduleac_Vlad Data 29 iunie 2011 18:27:38
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <stdio.h>

long P, N, N1, N2, N3, P2;

long P1(long N) {
	long P = 0;
	while(N != 0) {
		P += N / 5;
		N /= 5;
	}
	return P;
}

int main() {
	freopen("fact.in", "r", stdin);
	freopen("fact.out", "w", stdout);
	scanf("%ld", &P);
	if(P == 0){ printf("1"); P2 = -1;}
	else {
		N1 = 1;
		N2 = P + 1;
		/*for(N = 5; N <= P * 5; N += 5) {
			N1 = N;
			P1 = 0;
			while(N1 != 0) {
				P1 += N1 / 5;
				N1 /= 5;
			}
			if(P1 == P) {
				printf("%ld", N);
				P1 = -1;
				break;
			}
			if(P1 > P)
				break;
		}*/
		while(N1 < N2) {
			N3 = (N1 + N2) / 2;
			if(P1(N3 * 5) < P)
				N1 = N3;
			else if(P1(N3 * 5) > P)
				N2 = N3;
			else {
				printf("%ld", N3 * 5);
				P2 = -1;
				break;
			}
		}
	}
	if(P2 != -1) {
		printf("-1");
	}
	return 0;
}