Cod sursa(job #2719370)

Utilizator GheorgheBBalamatiuc Gheorghe GheorgheB Data 9 martie 2021 19:48:57
Problema Factorial Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.54 kb
#include <fstream>
using namespace std;

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

int main(){
	int p;
	fin >> p;
	int st = 1, dr = 100000000, m, poz, p1, s;
	while(st <= dr){
		p1 = 5, s = 0;
		m = (st + dr)/2;
		while(p1 <= m)
			s += m/p1, p1 *= 5;
		if(s == p){
			poz = m;
			break;
		}
		else
			if(p < s)
				dr = m - 1;
			else
				st = m + 1;
	}
	while(poz > 1){
		s = 0;
		p1 = 5;
		while(p1 <= (poz - 1))
			s += (poz - 1)/p1, p1 *=5;
		if(s == p)
			poz --;
		else
			break;
	}
	fout << poz;
}