Cod sursa(job #1710657)

Utilizator pimao2004Lupu Stefan Dragos pimao2004 Data 29 mai 2016 16:07:42
Problema Consecutive Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.16 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in ("consecutive.in");
ofstream out ("consecutive.out");
pair <unsigned int,unsigned int> v[100000];
unsigned int dif[1000000];
unsigned int poz[1000000];
short sum (unsigned int poz1,unsigned int poz2,unsigned int nr)
{
    unsigned long long s=0;
    s=(unsigned long long)poz2*(poz2+1)/2;
    if(poz1>1)
    s=s-(unsigned long long)poz1*(poz1-1)/2;
    if(s==nr)
    return 1;
    else if(s<nr) return 2;
    else
    return 0;
}
int main()
{
    unsigned int t,n,k=0;
    in>>t;
    unsigned int i,poz1,poz2;
    for(i=1;i<=t;i++)
    {
        in>>n;
        for(poz1=1;poz1<=n;poz1++)
        for(poz2=poz1+1;sum(poz1,poz2,n);poz2++)
        if(sum(poz1,poz2,n)==1)
        {
            v[k+1].first=poz1;
            v[k+1].second=poz2;
            dif[k+1]=poz2-poz1;
            poz[dif[k+1]]=k+1;
            k++;
        }
        out<<k<<'\n';
        sort(dif+1,dif+k+1);
        for(unsigned int j=1;j<=k;j++)
        {
            out<<v[poz[dif[j]]].first<<' '<<v[poz[dif[j]]].second<<'\n';
        }
        k=0;
        out<<'\n';
    }
    return 0;
}