Cod sursa(job #2927519)

Utilizator MatwueiBordei Cosmin Matwuei Data 20 octombrie 2022 19:22:03
Problema Factorial Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.53 kb
#include<bits/stdc++.h>

std::ifstream in("fact.in");
std::ofstream out("fact.out");

float log5(int x){
	return (float)log2(x)/log2(5);
}

float cat5(int x){
	float ret = 0;
	ret = (float)x/5;
	for(int i = 25; i<=x; i *= 5){
		ret ++;
	}
	return ret;
}

int main(){
	int n; in>>n;
	
	//binary search from 1 to MAX_INT aprox 32 steps, constant time searching
	unsigned int x = 0, y = INT_MAX;
	while(x < y){
		int m = (x+y)/2;
		if(cat5(m) > n) y = m - 1;
		else if(cat5(m) < n) x = m + 1;
		else{
			out<<m;
			
			return 0;
		}
	}
	out<<-1;
}