Cod sursa(job #1255404)

Utilizator andreas.chelsauAndreas Chelsau andreas.chelsau Data 4 noiembrie 2014 19:21:52
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <iostream>
using namespace std;
typedef long long ll;
ll p;
ll digits(ll n){
	ll r = 0;
	while(n > 4){
		r += n / 5;
		n /= 5;
	}
	return r;
}
int main(){
	freopen("fact.in","r",stdin);
	freopen("fact.out","w",stdout);

	scanf("%lld",&p);
	if(p == 0)
    {
        printf("1\n");
        return 0;
    }
	ll l = 1,r = 10000000000,mid;
	int k = 0;
	while(l <= r){
		mid = l + (r - l) / 2;
		if(digits(mid) > p)
			r = mid - 1;
		else
			l = mid + 1;
		if(digits(mid) == p){
			k = 1;
			break;
		}
	}
	if(k == 1)
		printf("%lld",mid - mid % 5);
	else
		printf("-1\n");
	return 0;
}