Matrix (二分套二分
2024-09-07 18:14:18
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 ;
}
最新文章
- SQL中字符串拼接
- context:property-placeholder
- poj2155 树状数组 Matrix
- Hibernate 环境搭建
- redis的主从复制与哨兵
- 【转】GPS误差来源
- Leetcode_36_Valid Sudoku
- 如何让EasyUI的Tree或者ComboTree节点不显示图标?
- Base64 加解密
- LVS初始使用步骤
- Wincc数据库连接代码生成方法
- python--linux上如何执行shell命令
- No write since last change (add ! to override)
- java代码示例(6-3)
- [转]获取app的内部储存路径
- 无法定位序数****于动态链接库LIBEAY32.dll上
- 迷你MVVM框架 avalonjs1.5.2 发布
- 【转载】WINAPI宏
- 让声音更清晰,用PR去掉视频中的噪音
- js this问题和es6箭头函数this问题
热门文章
- 使用反射机制恢复xml文件表示的对象
- WebRTC PeerConnection
- Java-Runoob-高级教程-实例-字符串:12. Java 实例 - 字符串优化
- 常见电商项目的数据库表设计(MySQL版)
- Android系统中setprop,getprop,watchprops命令的使用(转载)
- python 面向对象十 __init__和__new__
- bzoj 2208: [Jsoi2010]连通数【tarjan+拓扑+dp】
- apicloud踩坑集锦
- win7 右键菜单残影 消除方法
- [AHOI2007]密码箱