Cod sursa(job #1709188)

Utilizator utcn_roxanaalexfloUTCN ROXANA ALEX FLO utcn_roxanaalexflo Data 28 mai 2016 11:12:29
Problema Consecutive Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.08 kb
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <fstream>
#include <vector>

using namespace std;

vector<pair<int,int>> findSummations(int n) {
	bool hasSol = true;
	int numerator;
	int i = 2;
	vector<pair<int, int>> solutions;
	solutions.reserve((int) sqrt(n));

	while (hasSol) {
		hasSol = false;
		if (i * (i + 1) <= 2 * n) {
			hasSol = true;
			numerator = n - i * (i - 1) / 2;
			
			if (numerator % i == 0) {
				int k = numerator / i;
				solutions.push_back(make_pair(k, k + i - 1));
			}			
		}	 
		i++;
	}

	return solutions;
}

int main()
{
	ios::sync_with_stdio(false);
	
	ifstream inFile("consecutive.in");
	ofstream outFile("consecutive.out");

	int T;

	inFile >> T;
	int n; 

	for (int t = 0; t < T; t++) {
		inFile >> n;
		
		vector<pair<int,int>> solutions = findSummations(n);
		int size = solutions.size();
		outFile << size << endl;
		for (int i = 0; i < size; i++) {
			outFile << solutions[i].first << " " << solutions[i].second << endl;
		}		

	}

	inFile.close();
	outFile.close();

    return 0;
}