Cod sursa(job #1710055)

Utilizator UBB_Muresan_Nasca_StefanUBB Muresan Nasca Stefan UBB_Muresan_Nasca_Stefan Data 28 mai 2016 14:54:57
Problema Consecutive Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.98 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

long long n,m,i,j,t,e,sum,k, aux, times;

bool CMP(pair<long long,long long> T1, pair<long,long> T2)
{
    return T1.second - T1.first < T2.second - T2.first;
}

vector< pair<long long , long long> > a;
vector< pair<long long, long long> > b;
int main()
{

    ifstream fin("consecutive.in");
    ofstream fout("consecutive.out");
    fin>>t;
    for(e=1;e<=t;e++)
    {
        a.clear(); b.clear();
        int contor = 1;
        fin>>n;
        //int an = sqrt(n);
        int an = (-1 +  sqrt(1+8*n))/2;
        j = 1;
        aux = 2;
        times = 0;
        //for(i=n/contor + 1;i>=1;i--)
        while(j != aux || (times <= 10 && j == aux))
        {
            aux = n/contor;
            contor++;
            j = n/contor;
            //fout<<aux<<" "<<j<<"\n";

            if(j != aux)
            {
                times = 0;
                for(i=j + contor / 2 - 1; i < j + contor / 2 + 2;i++)
                {
                    sum = (i * (i + 1) / 2 - n) * 2;
                    k = (-1 + sqrt(1+4*sum))/2;
                    if(((i*(i+1)/2 - k*(k+1)/2) == n) && (i>0))
                    {
                        if(i != k + 1)
                        {
                            a.push_back({k+1,i});
                        }
                    }
                }
            }
            else
            {
                times ++;
            }
        }

        sort(a.begin(), a.end(), CMP);
        for (i = 0; i < a.size(); i++){
            if (i == 0){
                b.push_back(a[i]);
            } else {
                if (a[i] != a[i - 1]){
                    b.push_back(a[i]);
                }
            }
        }

        fout<<b.size()<<"\n";
        for(i=0;i<b.size();++i)
        {
            fout<<b[i].first<<" "<<b[i].second<<"\n";
        }
    }
    return 0;
}