Cod sursa(job #1756398)

Utilizator alex_bucevschiBucevschi Alexandru alex_bucevschi Data 12 septembrie 2016 19:31:36
Problema Progresie Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.18 kb
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ll long long
#define pii pair<int,int>
#define tii tuple <int,int,int>
#define N 200005
#define mod 1000000005
#define X first
#define Y second
#define eps 0.0000000001
#define all(x) x.begin(),x.end()
#define tot(x) x+1,x+n+1
using namespace std;

ll n,x,nx,l,ok,block,lf,r,i,j;
int t;


int main() {
    ifstream fin("progresie.in");
    ofstream fout("progresie.out");
    fin>>t;
    for(; t; t--) {
        fin>>n>>r;
        for(i=1;; i++) {
            x=i*(i-1)+1;
            l=i-1;
            ok=1;

            for(j=1; j<n; j++) {
                nx=x+r*j;
                block=sqrt(nx-1)+1;
                lf=block*(block-1)+1;
                if(nx<lf) {
                    l-=lf-nx;
                    x+=lf-nx;
                }
                if(l>block*block-nx)
                    l=block*block-nx;
                if(l<0) {
                    ok=0;
                    break;
                }
            }

            if(ok) {
                fout<<x<<'\n';
                break;
            }
        }
    }
    return 0;
}