Cod sursa(job #2540690)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 7 februarie 2020 15:12:18
Problema Progresie Scor 0
Compilator cpp-64 Status done
Runda Arhiva ICPC Marime 0.96 kb
#include <bits/stdc++.h>
#define nmax 1024
#define ll long long
using namespace std;
ifstream in("progresie.in");
ofstream out("progresie.out");
int n,t,r;
struct line{
    int x;
    int poz;
};
line ini;
int value(line a){
    return a.x*(a.x-1)+a.poz;
}
line nextLine(int val){
    int x = sqrt(val);
    if(x*(x+1)<val){
        return {x+1,val-x*(x+1)};
    }
    if(x*(x-1)<val){
        return {x,val-x*(x-1)};
    }
    if((x-1)*(x-2)<val){
        return {x-1,val-(x-1)*(x-2)};
    }
}
bool getNextLine(int val){
    line aux = ini;
    for(int i=1; i<n; i++){
        aux = nextLine(value(aux)+val);
        if(aux.poz>aux.x){
            ini = nextLine(value({aux.x+1,1}));
            return false;
        }
    }
    return true;
}
void solve()
{
    in >> n >> r;
    ini = {1,1};
    for(line a = ini;!getNextLine(r););
    out << value(ini) << '\n';

}
int main(){
    in >> t;
    while(t--){
        solve();
    }
}