Cod sursa(job #1709455)

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

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

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

    ifstream fin("consecutive.in");
    ofstream fout("consecutive.out");
    fin>>t;
    for(e=1;e<=10;e++)
    {
        a.clear();
        int contor = 2;
        fin>>n;
        j=n/contor;
        //for(i=n/contor + 1;i>=1;i--)
        while(j>=1)
        {
            //fout<<i<<"\n";
            for(i=j;i<min(j+contor, n /(contor-1));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)
                    {
                        if(a.size() > 0 && a[a.size()-1].first != k + 1 && a[a.size()-1].second != i)
                        {
                            a.push_back({k+1,i});

                        }
                        else if(a.size() == 0)
                        {
                            a.push_back({k+1,i});
                        }
                    }
                }
            }
            contor++;
            j=n/contor;
            if(j<sqrt(n)) break;
            //fout<<j<<" "<<contor<<"\n";
        }
        fout<<a.size()<<"\n";
        for(i=0;i<a.size();++i)
        {
            fout<<a[i].first<<" "<<a[i].second<<"\n";
        }
    }
    return 0;
}