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 paintedN? ×?MMultiplication 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? ≤?N,?M? ≤? 5 · 105; 1? ≤?K? ≤?N·M).
Output
PrintK-Th largest number inN? ×?MMultiplication 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? ×? 3 multiplication table looks like this:
1 2 32 4 6
Let's find the k-th largest number in a multiplication matrix of N * m.
Idea: Perform binary search to count the results of each row
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>typedef long long ll;using namespace std;ll n, m, k;int check(ll x) {ll res = 0;for (int i = 1; i <= n; i++) {ll tmp = min(i*m, x);res += tmp / i;}return res < k;}ll search(ll l, ll r) {while (l < r) {ll mid = (l + r) / 2;if (check(mid))l = mid + 1;else r = mid;}return r;}int main() {cin >> n >> m >> k;ll Right = n * m, Left = 1;ll ans = search(Left, Right);cout << ans << endl;return 0;}
Codeforces round #256 (Div. 2) D. Multiplication table