Cod sursa(job #1570515)

Utilizator tudormaximTudor Maxim tudormaxim Data 16 ianuarie 2016 16:29:57
Problema Progresie Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main()
{
    ifstream fin("progresie.in");
    ofstream fout("progresie.out");
    ios_base::sync_with_stdio(false);
    long long t, n, r, i, x, sol, m, w, st, dr, stt, drr;
    bool ok, okk;
    fin >> t;
    while(t--)
    {
        fin >> n >> r;
        okk=false;
        for (x=1; !okk; x++)
        {
            sol=x*(x-1)+1;
            m=x-1;
            ok=1;
            for(i=1; i<n && ok; i++)
            {
                st=sol+r*i;
                dr=st+m;
                w=1+(int)sqrt(st-1);
                stt=w*(w-1)+1;
                drr=w*w;
                if (dr < stt) ok=0;
                if (st < stt)
                {
                    m-=stt-st;
                    sol+=stt-st;
                }
                else if(dr > drr) m-=dr-drr;
            }
            if (ok)
            {
                fout << sol << "\n";
                okk=true;
            }
        }
    }
    fin.close();
    fout.close();
    return 0;
}