Matrix

http://poj.org/problem?id=3685

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 8943   Accepted: 2738

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

 #include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std; ll n,k;
ll cal(ll i,ll j){
return i*i+*i+j*j-*j+i*j;
} bool Check(ll num){
ll sum=;
for(ll i=;i<=n;i++){
ll L=,R=n,mid;
while(L<=R){
mid=L+R>>;
if(cal(mid,i)<=num){
L=mid+;
}
else{
R=mid-;
}
}
sum+=R;
}
return sum>=k;
} int main(){ int T;
cin>>T;
while(T--){
cin>>n>>k;
ll L=-1e18,R=1e18,mid;
while(L<=R){
mid=L+R>>;
if(Check(mid)){
R=mid-;
}
else{
L=mid+;
}
}
cout<<L<<endl;
} }

Matrix(二分套二分)的更多相关文章

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

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

  2. poj3579 二分套二分

    和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...

  3. POJ-3579 Median---二分第k大(二分套二分)

    题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...

  4. poj 3579 Median 二分套二分 或 二分加尺取

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5118   Accepted: 1641 Descriptio ...

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

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

  6. Matrix (二分套二分

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

  7. Matrix [POJ3685] [二分套二分]

    Description 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. Input 第一行输 ...

  8. poj3685 二分套二分

    F - 二分二分 Crawling in process... Crawling failed Time Limit:6000MS     Memory Limit:65536KB     64bit ...

  9. 二分套二分 hrbeu.acm.1211Kth Largest

    Kth Largest TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Description There are two sequences A and ...

随机推荐

  1. ASP.NET Web Pages:对象

    ylbtech-.Net-ASP.NET Web Pages:对象 1.返回顶部 1. ASP.NET Web Pages - 对象 Web Pages 经常是跟对象有关的. Page 对象 您已经看 ...

  2. [转]Windows服务“允许服务与桌面交互”的使用和修改方法

    上一篇文章是纯 C# 代码的形式勾上 “允许服务与桌面交互” 选项的 本文转载自:http://blog.csdn.net/lanruoshui/article/details/4756408 描述: ...

  3. 转载-JavaWeb学习总结

    JavaWeb学习总结(五十三)——Web应用中使用JavaMail发送邮件 孤傲苍狼 2015-01-12 23:51 阅读:13407 评论:20     JavaWeb学习总结(五十二)——使用 ...

  4. RF安装

    参考: http://www.cnblogs.com/zlj1992/p/6357373.html https://github.com/robotframework/RIDE/wiki/Instal ...

  5. python&pandas 与mysql 连接

    1. python 与mysql 连接及操作,直接上代码,简单直接高效: import MySQLdb try: conn = MySQLdb.connect(host='localhost',use ...

  6. Protocol Buffer Basics: Python

    原文https://developers.google.com/protocol-buffers/docs/pythontutorial Protocol Buffer Basics: Python ...

  7. 由一条普通的link引用引发的无数问号,大家能回答的帮忙回答回答吧.

    <link type="text/css" rel="stylesheet" href="1.css" /> 对于前台工作者来说 ...

  8. 学习MongoDB 一:MongoDB 入门(安装与配置)

    一.简介 MongoDB一种非关系型数据库(NoSql),是一种强大.灵活.可扩展的数据存储方式,因为MongoDB是文档模型,自由灵活很高,可以让你在开发过程中畅顺无比,对于大数据量.高并发.弱事务 ...

  9. JavaScript中‘==’和'==='的区别

    javascript中,两个等号‘==’和三个等号‘===’的区别: 简单说,‘===’比‘==’对相等的概念更为严格,使用‘==’时,数字 1 和 字符串 “1” 是相等的: 而使用‘===’时,数 ...

  10. JavaScript中判断函数、变量是否存在

    转载:http://www.jb51.net/article/67551.htm 一.是否存在指定函数 function isExitsFunction(funcName) { try { if (t ...