Cod sursa(job #1709725)

Utilizator UBB_RETIRED_BUT_DANGEROUSUBBTEAM UBB_RETIRED_BUT_DANGEROUS Data 28 mai 2016 13:37:52
Problema Consecutive Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.75 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <math.h>

using namespace std;

ifstream in("consecutive.in");
ofstream out("consecutive.out");

bool isPowerOfTwo(long long n)
{
	if ((n & (n - 1)) == 0)
		return 1;
	return 0;
}

bool cmp(pair<int, int> x, pair<int, int> y)
{
	if (x.first > y.first)
	{
		return true;
	}

	return false;
}

bool isPrime(int x)
{
	if (x == 2)
	{
		return true;
	}
	if (x % 2 == 0)
	{
		return false;
	}

	int right = sqrt(x);
	for (int i = 3; i <= right; i += 2)
	{
		if (x % i == 0)
		{
			return false;
		}
	}

	return true;
}

int main()
{
	int T; in >> T;
	for (int t = 0; t < T; t++)
	{
		int N; in >> N;
		vector<pair<int, int>> sol;

		if (isPowerOfTwo(N) || N == 1)
		{
			out << 0 << '\n';
			continue;
		}

		sol.push_back(make_pair(N / 2, N / 2 + 1));

		if (isPrime(N))
		{
			out << 1 << '\n';
			out << N / 2 << ' ' << N / 2 + 1 << '\n';
			continue;
		}

		set<int> divisors;
		int factor = 2;
		int coefpowfactor = 1;
		int current = N;
		while (current != 1)
		{
			while (current % factor == 0)
			{
				coefpowfactor *= factor;
				current /= factor;
				divisors.insert(coefpowfactor);
			}

			factor++;
			coefpowfactor = 1;
		}
		

		for (set<int>::iterator it = divisors.begin(); it != divisors.end(); it++)
		{
			int q = *it;

			int r = N / q;
			int left = r - q / 2;
			int right = r + q / 2;
			if (left < 1)
			{
				continue;
			}

			sol.push_back(make_pair(left, right));
		}

		// sort(sol.begin(), sol.end(), cmp);

		out << sol.size() << '\n';
		for (unsigned int i = 0; i < sol.size(); i++)
		{
			out << sol[i].first << ' ' << sol[i].second << '\n';
		}
	}

	return 0;
}