Pagini recente » Cod sursa (job #1047179) | Cod sursa (job #1580667) | Cod sursa (job #2299394) | Cod sursa (job #2545341) | Cod sursa (job #1721499)
#include <stdio.h>
#include <iostream>
#include <vector>
int power_of_5(int x, int p) {
//x / 5 ^ p
int p5 = 1;
for(int i = 1; i <= p ; ++i)
p5 *= 5;
return x / p5;
}
int zeros(int x) {
int cnt = 1;
int res = 0;
int sum = 0;
while( ( res = power_of_5(x, cnt) ) != 0 ) {
sum += res;
cnt++;
}
return sum;
}
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;
if(zeros(pos + 1) != P)
return -1;
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);
}