Cod sursa(job #2918471)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 11 august 2022 16:28:28
Problema Progresie Scor 100
Compilator cpp-64 Status done
Runda Arhiva ICPC Marime 0.63 kb
#include<bits/stdc++.h>
using namespace std;
ifstream F("progresie.in");
ofstream G("progresie.out");
int t;
long long n,r,i,v,l,b,x,y,e,j;
bool o;
int main()
{
    for(F>>t;t;--t)
        for(F>>n>>r,i=1;;++i) {
            for(v=i*(i-1)+1,e=i-1,o=1,j=1;j<n&&o;++j) {
                l=v+j*r,b=sqrt(l-1)+1,x=b*(b-1)+1,y=b*b;
                if(l<x)
                    v+=x-l,e-=x-l;
                else
                    e=min(e,y-l);
                if(l+e<x)
                    o=0;
            }
            if(o) {
                G<<v<<'\n';
                break;
            }
        }
    return 0;
}