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. wordpress 开发日志及技巧收集

    搜索结果数量提示 <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1" ...

  2. o2o的一些看法

    最近一段时间o2o(online to offline)真是越来越来热了,尤其是微信支持支付功能之后.现在随便哪个地方都可以看到二维码,然后手机一扫就可以看到跑到线上看到卖家一些产品信息了.看到这里面 ...

  3. 在WIN7下安装运行mongodb 1)、下载MongoDB

    1).下载MongoDB http://downloads.mongodb.org/win32/mongodb-win32-i386-2.4.5.zip 下载Windows 32-bit版本并解压缩, ...

  4. ios 中使用SBJson拼接和解析json

    1.ios解析json 使用开源json包,项目地址:      http://stig.github.com/json-framework/ NSData * responseData = [res ...

  5. TCP/IP,HTTP,Socket的区别与联系

    一 忆往昔,尽是悔恨泪.       在学校的时候学过,网络七层,也知道tcp的三次握手.但因为根本没用在实际开发中,所以逐渐淡忘.现在就再次理解下三个的区别与联系. 二 正题       网络七层: ...

  6. R语言 如何为图片添加文字说明(转载)

    转载:(中文翻译者)[http://blog.csdn.net/chen790646223/article/details/49766659] (原文链接)[http://datascienceplu ...

  7. 树莓派最简易Wifi配置

    树莓派最简易Wifi配置 相信我,连博客都会偷懒写个最简易给你看 前提,只有一根网线没有网络的前提下进行的. 基于Win10系统和树莓派2015-05-05-raspbian-wheezy.img测试 ...

  8. matlab 画不同图案的柱状图

    function applyhatch(h,patterns,colorlist) %APPLYHATCH Apply hatched patterns to a figure % APPLYHATC ...

  9. 软件工程随堂小作业——随机四则运算Ⅱ之算法思路(C++)

    1.题目避免重复: (1)利用系统时间来产生随机数,重复率会降低. (2)建立链表,逐个判断.可读取写入文件. 2.可定制(数量/打印方式): (1)格式有默认值; (2)可以选择重新设置分几列和每行 ...

  10. Mac下安装及配置Eclipse

    1.安装Eclipse前先确认你的Mac上是否已安装java运行环境.进入终端,输入”java -version”,如果返回了java版本号则说明已安装,否则,请先安装java运行环境: 2.访问ec ...