Cod sursa(job #1721488)

Utilizator elena.marinicaMarinica Elena-Georgiana elena.marinica Data 25 iunie 2016 18:57:07
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <stdio.h>
#include <iostream>
#include <vector>

std::vector<int> v;

int power_of_5(int x) {
	
	int p = 0;
	
	while (x / 5 > 0) {
		p++;
		x /= 5;
	}

	return p;
}

int zeros(int x) {
	return ((x / 5) + power_of_5(x) - 1);	
}

int binary_search(int P) {
	
	int pos ; int step;
	
	for(step = 1; step <= P * 5 ; step *= 2);
	
	for(pos = 0; step > 0; step /= 2)
		if(pos + step <= P * 5 && zeros(pos + step) < P)
			pos += step;
			
	return pos + 1;
}


int main() {
	
	int P;
	FILE *fin = fopen("fact.in", "r");
	FILE *fout = fopen("fact.out", "w");
	
	fscanf(fin, "%d", &P);
	
	
	int res = binary_search(P);
	
	fprintf(fout, "%d\n", res);
	
	fclose(fin);
	fclose(fout);
}