同行元素递减,同列元素递增,采用嵌套二分的方法
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef __int64 LL;
LL n, m;
LL F(LL l, LL r){
return l * l + 100000 * l + r * r - 100000 * r + l *r;
}
LL solve(LL x){
LL sum = 0;
for(int i = 1; i <= n; i++){
int l = 1, r = n + 1;
while(l < r){
LL mid = (l + r)>>1;
if(F(mid, i) > x){
r = mid;
}else{
l = mid + 1;
}
}
sum += l - 1;
}
return sum;
}
int main(){
int t;
cin>>t;
while(t--){
cin>>n>>m;
LL l = -100000*n, r = n*n+100000*n+n*n+n*n;
while(l < r){
LL mid = (l + r) >> 1;
if(solve(mid) < m){
l = mid + 1;
}else{
r = mid;
}
}
cout<<l<<'\n';
}
return 0;
}