Cod sursa(job #2686043)

Utilizator catalin69420Gogu Popescu catalin69420 Data 18 decembrie 2020 14:05:25
Problema Factorial Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>

using namespace std;

typedef unsigned long ulong;
typedef unsigned int uint;

uint count_factor(uint n, uint factor) {
	// count the how many times "factor" occurs
	// in the factor decomposition of n
	uint count = 0;
	while (n != 0 && n % factor == 0) {
		n /= factor;
		count++;
	}

	return count;
}

// find n
// where n is the smallest numbers
// for which n! ends with the given number of zeroes
uint find_sol(uint zeroes) {
	uint count2 = 0;
	uint count5 = 0;
	uint n = 0;

	while (min(count2, count5) < zeroes) {
		count2 += count_factor(n, 2);
		count5 += count_factor(n, 5);
		n++;
	}

	return n - 1;
}
int main(int argc, char** argv) {
	// read the number of zeroes from file
	ifstream f("fact.in");
	uint zeroes;
	f >> zeroes;

	ofstream out("fact.out");
	out << find_sol(zeroes);

	return 0;
}