Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i2 + 100000 × i + j2 - 100000 × j + i × j, you are to find the M-th smallest element in the matrix.

Input

The first line of input is the number of test case.
For each test case there is only one line contains two integers, N(1 ≤ N ≤ 50,000) and M(1 ≤ M ≤ N × N). There is a blank line before each test case.

Output

For each test case output the answer on a single line.

Sample Input

12

1 1

2 1

2 2

2 3

2 4

3 1

3 2

3 8

3 9

5 1

5 25

5 10

Sample Output

3
-99993
3
12
100007
-199987
-99993
100019
200013
-399969
400031
-99939

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t;
long long n,k;
long long make(long long i,long long j){
return i*i+j*j+i*-j*+i*j;
}
bool erfen(long long m){
long long cnt=;
for(int j=;j<=n;j++){
int l=,r=n,ans=;
while(l<=r){//内层二分
int i=l+r>>;
if(make(i,j)<=m){
ans=i;
l=i+;
}
else r=i-;
}
cnt+=ans;
}
return cnt>=k;
}
int main(){
cin>>t;
for(int w=;w<t;w++){
cin>>n>>k;
long long l=-*n;
long long r=n*n+n*n+*n+n*n,ans;
while(l<=r){//外层二分
long long m=l+r>>;
if(erfen(m)){
ans=m;
r=m-;
}else l=m+;
}
cout<<ans<<endl;
}
return ;
}

最新文章

  1. SQL中字符串拼接
  2. context:property-placeholder
  3. poj2155 树状数组 Matrix
  4. Hibernate 环境搭建
  5. redis的主从复制与哨兵
  6. 【转】GPS误差来源
  7. Leetcode_36_Valid Sudoku
  8. 如何让EasyUI的Tree或者ComboTree节点不显示图标?
  9. Base64 加解密
  10. LVS初始使用步骤
  11. Wincc数据库连接代码生成方法
  12. python--linux上如何执行shell命令
  13. No write since last change (add ! to override)
  14. java代码示例(6-3)
  15. [转]获取app的内部储存路径
  16. 无法定位序数****于动态链接库LIBEAY32.dll上
  17. 迷你MVVM框架 avalonjs1.5.2 发布
  18. 【转载】WINAPI宏
  19. 让声音更清晰,用PR去掉视频中的噪音
  20. js this问题和es6箭头函数this问题

热门文章

  1. 使用反射机制恢复xml文件表示的对象
  2. WebRTC PeerConnection
  3. Java-Runoob-高级教程-实例-字符串:12. Java 实例 - 字符串优化
  4. 常见电商项目的数据库表设计(MySQL版)
  5. Android系统中setprop,getprop,watchprops命令的使用(转载)
  6. python 面向对象十 __init__和__new__
  7. bzoj 2208: [Jsoi2010]连通数【tarjan+拓扑+dp】
  8. apicloud踩坑集锦
  9. win7 右键菜单残影 消除方法
  10. [AHOI2007]密码箱