Cod sursa(job #3036613)

Utilizator tudorcaluTudor Calu tudorcalu Data 24 martie 2023 18:07:07
Problema Consecutive Scor 0
Compilator cpp-64 Status done
Runda Arhiva ICPC Marime 1.1 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

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

vector<pair<int,int>> find_consecutive_sum(int N) {
    vector<pair<int,int>> solutions;
    int start = 1;
    while (start <= N / 2) {
        int end = start + 1;
        int total = start + end;
        while (total <= N) {
            if (total == N) {
                solutions.push_back(make_pair(start, end));
                break;
            }
            end++;
            total += end;
        }
        start++;
    }
    return solutions;
}

int main() {
    
    int T;
    f >> T;
    
    while (T){
    
    int N;
    f >> N;
    vector<pair<int,int>> solutions = find_consecutive_sum(N);
    sort(solutions.begin(), solutions.end(), [](pair<int,int> a, pair<int,int> b) {
        return b.second - b.first < a.second - a.first;
    });
    g << solutions.size() << endl;
    for (auto it = solutions.rbegin(); it != solutions.rend(); it++) {
        g << it->first << " " << it->second << endl;
    }
    
    T--;
    }
    return 0;
}