Cod sursa(job #950506)

Utilizator dpopovicDana Popovici dpopovic Data 17 mai 2013 00:16:24
Problema Factorial Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

long get_count(long val) {
	long count=0, i = 5;

	while( i <= val ) {
		count += val/i;
		i *= 5;
	}
	return count;
}

long get_N(long P) {
	long min = 4*P, max = 5*P, mid, zeros;
	while ( min <= max) {
		mid = (min + max)/2;
		zeros = get_count(mid);
		printf("min=%ld   max=%ld   zeros=%ld\n", min, max, zeros );
		if(zeros==P)
			return (mid - mid%5);
		if(zeros>P) {
			max = mid-1;
		} else {
			min = mid+1;
		}
		if(min>max)
			return -1;
	}
	return -1;
}

int main(int argc, char *argv[]) {
	
	long P, N;
	
	clock_t start = clock();
	
	FILE *f1 = fopen("fact.in", "r");
	fscanf(f1, "%ld", &P);
	fclose(f1);	

	f1 = fopen("fact.out", "w");
	
	N = get_N(P);
	printf("get_count=%ld\nget_N=%ld\n", get_count(P), N);
	
	fprintf(f1, "%ld\n", P>0?N:1);
	fclose(f1);	
	
	printf ( "%f ms\n\n", ( (double)clock() - start ) * 1000 / CLOCKS_PER_SEC);
	
	return 0;
}