Cod sursa(job #1709315)

Utilizator UBB_RETIRED_BUT_DANGEROUSUBBTEAM UBB_RETIRED_BUT_DANGEROUS Data 28 mai 2016 11:46:49
Problema Consecutive Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.59 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <fstream>

using namespace std;
bool isPowerOfTwo(int n)
{
	if ((n & (n - 1)) == 0)
		return 1;
	return 0;
}

ifstream in("consecutive.in");
ofstream out("consecutive.out");
#define dim 1000

bool cmp(pair<int, int> x, pair<int, int> y)
{
	if ((y.second - y.first) > (x.second - x.first))
		return true;
	return false;
}
int main()
{
	int T;
	in >> T;
	for (; T; --T)
	{
		pair < int, int > sol[dim];
		int m = 0;
		int n;
		in >> n;
		if (isPowerOfTwo(n) || n == 1)
		{
			out << 0 << '\n';
			continue;
		}
		if (n % 3 == 0 && n > 3)
		{
			int rest = (n / 3);
			sol[++m] = make_pair(rest - 1, rest + 1);
			//raspunsul e (n-1,n+1, unde n e n/3)
		}
		if (n % 5 == 0 && n > 10)
		{
			int rest = (n / 5);
			sol[++m] = make_pair(rest - 2, rest + 2);
			//raspunsul e (n-2,n+2)
		}
		if (n % 7 == 0 && n > 21)
		{
			int rest = (n / 7);
			sol[++m] = make_pair(rest - 3, rest + 3);
			// rasp e (n-3, n+3)
		}
		if (n % 9 == 0 && n > 36)
		{
			int rest = (n / 9);
			sol[++m] = make_pair(rest - 4, rest + 4);
			//rasp e (n-4,n+4);	
		}
		if (n % 2 == 1 && n > 1)
		{
			int rest = n / 2;
			sol[++m] = make_pair(rest, rest + 1);
			//rasp e n/2, n/2 + 1
		}
		if (n % 4 == 2 && n > 6)
		{
			int rest = n / 4;
			sol[++m] = make_pair(rest - 1, rest + 2);
		}

		out << m << '\n';
		sort(sol + 1, sol + m + 1, cmp);
		for (int i = 1; i <= m; ++i)
			out << sol[i].first << " " << sol[i].second << '\n';
		//out << "gata testu" << '\n';
	}

	return 0;
}