https://codeforces.com/problemset/problem/448/D
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.
Examples
input
Copy
2 2 2
output
Copy
2
input
Copy
2 3 4
output
Copy
3
input
Copy
1 10 5
output
Copy
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3 2 4 6
最开始看着题没啥思路,在草稿纸上画着画着感觉从小到大每一个数有多少次出现是可以通过因子算出来的。于是想直接暴力,大致复杂度O(ksqrt(k)).[毫无疑问肯定超时]
附上T8代码:
#pragma GCC optimize("Ofast")//优化开关
#pragma GCC target("avx,avx2,fma")//指令集
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
LL n,m,k;
LL check(LL x){
LL ans=0;
for(LL i=1;i<=sqrt(x);i++){
if(x%i==0&&i!=sqrt(x)){
if(i<=n&&x/i<=m) ans++;
if(i<=m&&x/i<=n) ans++;
}
else if(i*i==x){
if(i<=m&&i<=n) ans++;
}
}
return ans;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
cin>>n>>m>>k;
LL sum=0;
for(LL i=1;i<=k;i++){
sum+=check(i);
if(sum>=k){
cout<<i<<endl;
break;
}
}
return 0;
}
这道题考虑的点:
找的第k个数在1-n*m的范围里是连续的并且是有单调性的,所以可以往二分思考。碰到范围连续且单调序列可以先往二分思考
假设写好了check,那么枚举的数mid满足的check的话,最后要求的数必然<=mid,所以是r=mid;
考虑check,原来这么写是直接暴力分解因子。题目给元素值的n*m,其实要想求出mid前有多少数,只要知道每行有多少小于等于mid的就好了。mid由题目而来必然是n和m的因数,观察可以发现mid/i(i:1~n)可以知道每行有多少数满足,然后累加就可以了。
#pragma GCC optimize("Ofast")//优化开关
#pragma GCC target("avx,avx2,fma")//指令集
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
LL n,m,k;
bool check(LL mid){
LL sum=0;
for(LL i=1;i<=n;i++){
sum+=min(m,mid/i);
}
return sum>=k;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
cin>>n>>m>>k;
LL l=1;LL r=n*m;
while(l<r){
LL mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<l<<endl;
return 0;
}