Poj3685 (nested binary)

Source: Internet
Author: User

Matrix
Time limit:6000 ms   Memory limit:65536 K
Total submissions:4658   Accepted:1189

Description

GivenN×NMatrix A, whose element inI-Th row andJ-Th ColumnAIJIs an number that equalsI2 + 100000 ×I+J2-100000 ×J+I×J, You are to findM-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) andM(1 ≤MN×N). There is a blank line before each test case.

Output

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

Sample Input

121 12 12 22 32 43 13 23 83 95 15 255 10

Sample output

3-99993312100007-199987-99993100019200013-399969400031-99939

Source

Poj founder monthly contest-2008.08.31, windy7926778 first create a table, and initially think it will increase progressively from bottom left to top right. Taking it seriously, it is not a special rule. When n is relatively large, it will increase. However, the monotonicity of each column can be maintained. It is easy to know how to take I and j as constants to find the derivative. This two points are interesting. In the long range, a number X is divided into two parts, and the number> is the number that satisfies the condition that X is greater than or equal to m and greater than M. It can be determined by dividing each column into two parts. Time Complexity log (10 ^ 18) * n * log (n ).
#include <iostream>using namespace std;long long N, M;const long long INF = 1LL << 50;long long mtr ( long long  i, long long j ){return i * i + 100000 * i + j * j - 100000 * j + i * j;}bool b_s ( long long X ){long long res = 0;for ( int i = 1; i <= N; i++ ){int cnt = N ;int l = 1, r = N;while ( l <= r ){int mid = ( r + l ) >> 1;if ( mtr ( mid, i ) >= X ){r = mid - 1;cnt = mid - 1;}else{l = mid + 1;}}res += cnt ;}return res >= M;}int main(){int n;cin >> n ;while ( n-- ){cin >> N >> M;long long l = -INF, r = INF;long long ans=-1;while ( l <= r ){long long mid = ( r + l )>>1;if ( b_s ( mid ) ){r = mid - 1;ans = mid - 1;}else{l = mid + 1;}}cout <<ans << endl;}return 0;}




Poj3685 (nested binary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.