Cod sursa(job #1745922)

Utilizator wilson182Alexandrina Panfil wilson182 Data 22 august 2016 15:23:50
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.64 kb
#include <bits/stdc++.h>
using namespace std;
int n, p, c, s, d;
int t[1000005];
int f(int x) {
	int nr1= 0;
	int y = 5;
	if (x < 5) return 0; 
	while (x >= y){
		nr1 = nr1 + x / y;
		y *= 5;
	}
	return nr1;
}
int main(){
	int nr;
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    cin>>p;
    if (p == 0) {
    	cout<< 1;
    	return 0;
	}
    s = 1;
    d = 1000000000;
    while (s < d) {
    	c = (s + d)/2;
    	nr = f(c);
    	if (nr == p) {
    		c -= c % 5;
    		printf("%d", c);
    		return 0;
		}
		if (nr < p) s = c + 1;
		else (d = c);
	}
    printf("%d\n", -1);
    return 0;
}