Cod sursa(job #1268663)

Utilizator radudorosRadu Doros radudoros Data 21 noiembrie 2014 11:37:34
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 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()
{
	bool h = 1;

	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--;
		}
		if (j!=12 && (p == nr[j+1] - 1))
			h = 0;
		p -= nr[j];
		q.push(j);
	}
	int sol=0;
	while (!q.empty())
	{
		sol += pow(5, q.front());
		q.pop();
	}
	if (h==1)
	fout << sol;
	else { fout << "-1"; }
}