Cod sursa(job #1725265)

Utilizator valentin50517Vozian Valentin valentin50517 Data 5 iulie 2016 12:35:48
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll P,N,E[100];
ll check(ll a){
	if(a<5) return 0;
	ll k = log(a)/log(5),rs = 0;
	rs+=(a-E[k])/5+k;
	while(--k)rs+=(E[k+1]-E[k])/5+k-1;
//	cout <<a << ' ' <<  rs << '\n';
	return rs;
}
int main(){
	ifstream cin("fact.in");
	ofstream cout("fact.out");
	E[0] = 1;
	for(int p = 1;p<=26;p++) E[p]=E[p-1]*5;
	cin >> P;
	if(P==0) return cout << 1,0;
	ll step,rs;
	for(step = rs = (1LL<<60);step;step>>=1)
		if(check(rs-step) >= P) rs-=step;
	if(check(rs) != P) cout << -1;else
	cout << rs << '\n';
    return 0;
}