Cod sursa(job #1268653)

Utilizator radudorosRadu Doros radudoros Data 21 noiembrie 2014 11:21:43
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;

queue <int> q;

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

long long nr[13];

int main()
{
	int p;
	fin >> p;
	int aux = p;
	nr[0] = 0;
	nr[1] = 1;
	for (int i = 2; i <= 12; i++)
	{
		nr[i] = nr[i - 1] * 5 + 1;
	}
	int j = 12;
	if (p == 0)
	{
		q.push(0);
	}
	while (p > 0)
	{	
		while (nr[j] > p)
		{
			j--;
		}
		p -= nr[j];
		q.push(j);
	}
	int sol=0;
	while (!q.empty())
	{
		sol += pow(5, q.front());
		q.pop();
	}
	int aux2 = sol;
	int c=0;
	while (aux2 % 5!=0)
	{
		aux2 /= 5;
		c++;
	}
	if (c==aux)
	fout << sol;
	else { fout << "-1"; }
}