Multiplication Table
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 painted ann × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-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 all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication 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 3 2 4 6
-----------------------------------editorial----------------------------------------
Solution is discreat binary search. We need to find largest x such that amount of numbers from table, least than x, is strictly less than k.
suppose by descreat b search we set that let answer is X than count no of numbers in each row <=X
this can easily done in o(1) in each row (by just devide )
now value x for which exactly k no of numbers are less than x will be the answer
main problem is redendency
---ex suppose input is 3,5,9
soo if create array and sor it it will be like
1 2 2 3 3 4 4 5 6 6 8 9 1014 15
now for k=9 ans is 6
but when we count using our method no of numbers <=6 than it will give 10, since 6 repeats 2 times (internal repeats will never affact answer ),
so to handle this problem we count no of times 6 can be form in the given array (factorise till sqrt), in this case it is 2(let z) and count <=6 is 10
soo answer for k=10-z+1 to 10 will be 6
------------------------------------------------------------------code--------------------------------------------------------------------
#include<iostream>#include<bits/stdc++.h>using namespace std;typedef long long int lli;lli ans=1e14;lli calc(lli n,lli m ,lli num){lli maxi=sqrt(num);int re=0;for(int i=1;i<=maxi;i++){if(num%i==0){lli se=num/i;if(i<=n && se<=m){re++;}if(i<=m && se<=n){re++;}if(se==i && se<=n && se<=m){re--;}}}return re;}int main(){lli n,m,k;cin>>n>>m>>k;lli mini=0;lli maxi=n*m+2;int s=0;//discreatwhile(mini<maxi){lli temp=0;lli mid=(mini+maxi)/2;lli re=calc(n,m,mid);// no of times mid can be formed in the given arrayfor(int i=1;i<=n;i++){temp+=min(mid/i,m);}lli r1=temp-re+1;lli r2=temp;if(k<=r2 && k>=r1){ans=min(ans,mid);}if(temp>=k){maxi=mid;}else{mini=mid+1;}}cout<<ans<<endl;}