可以看出每一列的数字都是单调的,于是就能在每一列用二分来计算一个值在这个矩阵中的排名。
二分答案+验证即可。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstdio>
#include <algorithm>
#define N 1005
typedef long long LL;
using namespace std;
int n;
LL M;
LL num(LL i , LL j)
{
return i * i + 100000LL * i + j * j - 100000LL * j + i * j;
}
void work()
{
LL i , j;
cin >> n >> M;
LL l = -2499849999LL , r = 7500000000LL , m;
while (l < r)
{
m = (l + r) >> 1;
LL sum = 0;
for (i = 1 ; i <= n ; ++ i)
{
LL top = 1 , bot = n + 1 , mid;
while (top < bot)
{
mid = (top + bot) >> 1;
if (num(mid , i) > m)
bot = mid;
else top = mid + 1;
}
sum += bot - 1;
}
if (sum < M)
l = m + 1;
else r = m;
}
cout << l << endl;
}
int main()
{
int _; scanf("%d",&_); while(_--)
work();
return 0;
}