Codeforces Round #256 (Div. 2) D Multiplication Table (二分)

D. Multiplication Table
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 painted an n × 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



题意:给你一个n和m,还有一个k,创建一个矩阵,点值等于i*j,问排序后第k个数字是多少

例如:n=2,m=3,k=4
矩阵为1 2 3
      2 4 6
排序后为1 2 2 3 4 6,第4个是3

思路:二分[0,n*m],主要的是找到小于等于x的数有多少,x/i如果大于等于m,那么表明第i行的数都小于x,如果x/i
小于m,那么表明第i行小于x的数有x/i个,然后二分查找小于x的数共有k个的那个数就行了。



ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
ll n,m,k;
ll fun(ll x)
{
	ll cnt=0;
	for(ll i=1;i<=n;i++)
	{
		if(i>x)
		break;
		ll q=x/i;
		if(q>=m)
		cnt+=m;
		else
		cnt+=q;
	}
	return cnt;
}
int main()
{
	while(scanf("%I64d%I64d%I64d",&n,&m,&k)!=EOF)
	{
		ll low=0,high=n*m;
		while(low<high)
		{
			ll mid=(low+high)/2;
			if(fun(mid)>=k)
			high=mid;
			else
			low=mid+1;
		}
		printf("%I64d\n",low);
	}
	return 0;
}


 

你可能感兴趣的:(Codeforces Round #256 (Div. 2) D Multiplication Table (二分))