Matrix
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 4428   Accepted: 1102

Description

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

Source

 
首先二分第m小的数,由公式易得每列的数是按照从小到大排列的,因此可二分,两次二分即可。
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std; typedef long long ll; int n;
ll m; ll cal(ll i,ll j) {
return i * i + j * j + (i - j) * + i * j;
} bool judge(ll x) {
ll sum = ;
for(int j = ; j <= n; ++j) {
int l = ,r = n;
while(l < r){
int mid = (l + r + ) / ;
if(cal(mid,j) <= x) {
l = mid;
} else {
r = mid - ;
}
}
sum += l;
} return sum >= m; } void solve() {
ll l = -1e12,r = 1e12; //printf(" n = %d l = %lld r = %lld\n",n,l,r); while(l < r) {
ll mid = (l + r) >> ;
if(judge(mid)) r = mid;
else l = mid + ;
} printf("%I64d\n",l);
} int main() {
// freopen("sw.in","r",stdin); int t;
scanf("%d",&t); while(t--) {
scanf("%d%I64d",&n,&m);
solve();
} return ;
}

POJ 3685的更多相关文章

  1. poj 3685 Matrix(二分搜索之查找第k大的值)

    Description Given a N × N matrix A, whose element × i + j2 - × j + i × j, you are to find the M-th s ...

  2. poj 3685 二分

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7415   Accepted: 2197 Descriptio ...

  3. POJ 3685 Matrix (二分套二分)

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8674   Accepted: 2634 Descriptio ...

  4. poj 3685 Matrix 二分套二分 经典题型

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 5724   Accepted: 1606 Descriptio ...

  5. Divide and conquer:Matrix(POJ 3685)

    矩阵 题目大意:矩阵里面的元素按i*i + 100000 * i + j*j - 100000 * j + i*j填充(i是行,j是列),求最小的M个数 这一题要用到两次二分,实在是二分法的经典,主要 ...

  6. POJ 3685 二分套二分

    Matrix Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that e ...

  7. poj 3685 Matrix 【二分】

    <题目链接> 题目大意: 给你一个n*n的矩阵,这个矩阵中的每个点的数值由   i2 + 100000 × i + j2 - 100000 × j + i × j  这个公式计算得到,N( ...

  8. POJ 3685 Matrix 二分 函数单调性 难度:2

      Memory Limit: 65536K Total Submissions: 4637   Accepted: 1180 Description Given a N × N matrix A, ...

  9. poj 3685 矩阵问题 查找第K小的值

    题意:N阶矩阵Aij= i2 + 100000 × i + j2 – 100000 × j + i × j,求第M小的元素. 思路:双重二分 考虑到,aij是跟着i递增的,所以i可以作为一个二分搜索 ...

随机推荐

  1. jpg图片在IE6、IE7和IE8下不显示解决办法

    坑人的IE浏览器,花了我一个小时才找到原因. 原因:IE内核不能渲染CMYK模式的jpg图片,需要转换为RGB模式. 在photoshop里点击菜单栏—图像—RGB模式就行了 引用:http://lg ...

  2. EMVTag系列6《IAC 发卡行行为代码》

    R(需求):数据应存在,在读应用数据过程中,终端不检查:将变成必备 L: 5 IAC 的值,最好不要自定义,最好和所选的模板完全匹配.如果修改持卡人认证位,PIN Bypass 相关的位.如果有业务需 ...

  3. “猜你喜欢”是怎么猜中你心思的?

    文/Joseph A. Konstan & John Riedl)如今,到网上购物的人已经习惯了收到系统为他们做出的个性化推荐.Netflix 会推荐你可能会喜欢看的视频.TiVo 会自动把节 ...

  4. linux打开端口

    客户那边有台服务器同一个局域网中都无法访问,排除lamp环境问题,发现时服务器中的防火墙没有开启80端口.于是去网上搜索了一下,在脚本之家看到一种添加代码的方法  代码如下 复制代码 vi /etc/ ...

  5. 【转载】FPGA静态时序分析——IO口时序

    转自:http://www.cnblogs.com/linjie-swust/archive/2012/03/01/FPGA.html 1.1  概述 在高速系统中FPGA时序约束不止包括内部时钟约束 ...

  6. forword属性

    forword属性 2013年7月8日 15:07 Name: Forward的名字,与mapping.findForward方法传入的值相同. Path: 请求转发的页面路径 Redirect: 请 ...

  7. ABAP后台JOB数量控制

    数据库视图:V_OP 可以查看JOB信息 FORM sub_check_job. * 通过JOB名称,控制活动JOB的数量 , jobname TYPE btcjob , strtdate TYPE ...

  8. 微软职位内部推荐-Senior Software Engineer-News

    微软近期Open的职位: News is a critical areas for integration of mobile and services, one of the top priorit ...

  9. VS2013 help viewer搜索结果显示源码以及桌面独立运行help viewer

    安装好VS2013后,启动help viewer2.1在搜索栏中搜搜时结果会出现HTML源码. 要解决这个问题先来看看MINE,即Multipurpose Internet Mail Extensio ...

  10. android开发 WriteUTF与readUTF 原理

    今晚上写代码玩,用到java.io.RandomAccessFile.writeUTF(String)函数,而文件默认保存为gbk,显然是乱码.突然想起来去看看存储编码规则,就去找了些文章了解writ ...