Cod sursa(job #362289)

Utilizator digital_phreakMolache Andrei digital_phreak Data 8 noiembrie 2009 19:46:29
Problema Factorial Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("fact.in");
ofstream fout("fact.out");

typedef long long ll;

ll five[25];
	
ll Z(ll x) {
	ll res = 0;
	ll i = 1;
	while ((x/five[i]) != 0) {
		res += x/five[i];
		i++;
	}
	return res;
}

void gen_five() {
	int i;
	five[0] = 1;
	for (i=1;i<=25;++i)
		five[i] = 5 * five[i-1];
}

int main() {
	
	ll N,P;
	
	ll x = 1;
	ll l;
	
	gen_five();
	
	fin >> P;
	
	for (l=1;Z(x+l)<P;l<<=1,x+=l);
	for (;l;l>>=1) {
		if (Z(x+l) < P) x += l;
	}
	
	if (P > 0) x += 1;
	if (Z(x) != P) x = -1;
	fout << x << endl;
	
	fin.close();
	fout.close();
	return 0;
}