Codeforces Round #256 (Div. 2) D binary answer,

Source: Internet
Author: User

Codeforces Round #256 (Div. 2) D binary answer,

D. Multiplication Tabletime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Bizon the Champion isn't just charming, he also is very smart.

While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion paintedNLimit × limitMMultiplication table, where the element on the intersection ofI-Th row andJ-Th column equalsI·J(The rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table isK-Th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?

Consider the given multiplication table. If you write out allN·MNumbers from the table in the non-decreasing order, thenK-Th number you write out is calledK-Th largest number.

Input

The single line contains integersN,MAndK(1 digit ≤ DigitN, Bytes,MLimit ≤ limit 5 · 105; 1 limit ≤ limitKLimit ≤ limitN·M).

Output

PrintK-Th largest number inNLimit × limitMMultiplication table.

Sample test (s) input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note

A 2 rows x rows 3 multiplication table looks like this:

1 2 32 4 6

Question

The question is to select the k decimal number from an n * m multiplication table (do not ask me what the multiplication table is) (the same number is calculated multiple times ).

Example 2 3 4

The multiplication table is

1 2 3

2 3 4

The non-subtraction sequence is: 1, 2, 2, 3, 3, 4. The output value is 3 because the number is 3.

At first, I thought of searching. I started searching from n * m and found that there were too many statuses. Even if it was a search, the time complexity was O (N * M ).

The correct solution is binary. The bipartite answer (boundary is [1, n * m]), and then find a smaller number in the multiplication table. Because the multiplication table is a regular number table, we can calculate O (1) for each column for a total of N times.

The total time complexity is O (N * 2 * log (N )).

Sample Code
/*************************************** * *********************************** # COPYRIGHT NOTICE # Copyright (c) 2014 All rights reserved # ---- Stay Hungry Stay Foolish ---- ##@ author: Shen # @ name: D # @ file: D. cpp # @ date: # @ algorithm: binary Search ************************************** **************************************** /// # pragma GCC optimize ("O2 ") // # pragma comment (linker, "/STA CK: 1024000000,1024000000 ") # include <bits/stdc ++. h> using namespace std; template <class T> inline bool updateMin (T & a, T B) {return a> B? A = B, 1: 0;} template <class T> inline bool updateMax (T & a, T B) {return a <B? A = B, 1: 0;} typedef long int64; int64 n, m, k; bool check (int64 x) {int64 res = 0; for (int I = 1; I <= n; I ++) {int64 tmp = min (I * m, x); res + = tmp/I;} return res <k ;} // count from small to large, the k int64 BinarySearch (int64 l, int64 r) {while (l <r) {int64 mid = (l + r)/2; // cout <l <"" <mid <"" <r <endl; // cout <"check result: "<check (mid); if (check (mid) l = mid + 1; else r = mid; // system (" pause ") ;}return r ;} int main () {cin> n> m> k; int64 Right = n * m, Left = 1; int64 ans = BinarySearch (Left, Right ); cout <ans; return 0 ;}





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.