Cod sursa(job #1709028)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 28 mai 2016 10:38:00
Problema Consecutive Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.57 kb
#include <bits/stdc++.h>

#define NMax 100000
#define ll long long
using namespace std;
ifstream f("consecutive.in");
ofstream g("consecutive.out");

struct ans{
    int prim,ultim;
}sol[NMax];

int t,n,nr;

int main()
{
    f >> t;
    for(int count = 1; count <= t; ++count){
        f >> n;
        nr = 0;
        for(int i = 2; 1LL * i * i <= 1LL * 2 * n;i ++){

            int lo = 1, hi = n / 2;
            while(lo <= hi){
                int mij = (lo + hi)/2;
                ll w = mij * i + 1LL * (i - 1) * i / 2;
                if(w == n){
                    sol[++nr].prim = mij;
                    sol[nr].ultim = mij + i - 1;
                    break;
                }else
                if(w < n){
                    lo = mij + 1;
                }else
                if(w > n){
                    hi = mij - 1;
                }
            }
           /*
            while(lo <= hi){
                int mij = (lo + hi)/2;
                if((mij + mij + i - 1) * i / 2 == n){
                    sol[++nr].prim = mij;
                    sol[nr].ultim = mij + i - 1;
                    break;
                }else
                if((mij + mij + i - 1) * i / 2 < n){
                    lo = mij + 1;
                }else
                if((mij + mij + i - 1) * i / 2 > n){
                    hi = mij - 1;
                }
            }   */
        }
        g << nr << '\n';
        for(int i = 1; i <= nr; ++i){
            g << sol[i].prim << ' ' << sol[i].ultim << '\n';
        }
    }
    return 0;
}