#256 (Div. 2)D. Multiplication Table

Posted 早知如此绊人心,何如当初莫相识。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#256 (Div. 2)D. Multiplication Table相关的知识,希望对你有一定的参考价值。

题意:给出n,m,k,问(1.....n)*(1......m)共有n*m个答案,问第k小

思路:很熟悉的样子,和51nod的第K大类似

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=500004;
 5 
 6 ll n,m,k;
 7 ll a[N],b[N];
 8 
 9 ll hh(int y,ll x){
10     int l=1,r=m,mid;
11     int ans=1;
12     while(l<=r){
13             mid=(l+r)>>1;
14         if(a[y]*b[mid]>=x){
15             ans=mid;r=mid-1;
16         }
17         else l=mid+1;
18     }
19     return ans;
20 }
21 ll check(ll x){
22     ll sum=0;
23     for(int i=1;i<=n;i++){
24         if(a[i]*b[m]<x) continue;
25         sum+=m-hh(i,x)+1;
26     }
27     return sum;
28 }
29 int main(){
30     scanf("%I64d%I64d%I64d",&n,&m,&k);
31     for(int i=1;i<=n;i++){
32         a[i]=i;
33     }
34     for(int i=1;i<=m;i++) b[i]=i;
35     ll l=a[1]*b[1],r=a[n]*b[m];
36     ll ans=0;
37     k=n*m-k+1;
38 
39     while(l<=r){
40         ll mid=(l+r)>>1;
41       //  cout<<mid<<" "<<check(mid)<<endl;
42         if(check(mid)>=k){
43             l=mid+1;
44             ans=mid;
45         }
46         else r=mid-1;
47 
48     }
49     printf("%I64d\n",ans);
50 }

不过这题不用再套一个二分,上面的跑了900多MS,差点GG

这个280MS

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=500004;
 5 
 6 ll n,m,k;
 7 ll a[N],b[N];
 8 
 9 ll check(ll x){
10     ll sum=0;
11     ll y;
12     for(int i=1;i<=n;i++){
13         y=min(x,i*m);
14         sum+=y/i;
15     }
16     return sum;
17 }
18 int main(){
19     scanf("%I64d%I64d%I64d",&n,&m,&k);
20     for(int i=1;i<=n;i++){
21         a[i]=i;
22     }
23     for(int i=1;i<=m;i++) b[i]=i;
24     ll l=a[1]*b[1],r=a[n]*b[m];
25     ll ans=0;
26 
27 
28     while(l<=r){
29         ll mid=(l+r)>>1;
30         //cout<<mid<<" "<<check(mid)<<endl;
31         if(check(mid)>=k){
32             r=mid-1;
33             ans=mid;
34         }
35         else l=mid+1;
36 
37     }
38     printf("%I64d\n",ans);
39 }

 

以上是关于#256 (Div. 2)D. Multiplication Table的主要内容,如果未能解决你的问题,请参考以下文章

D. Binary Literature_Codeforces Round #715 (Div. 2)

(容斥)Codeforces Round #428 (Div. 2) D. Winter is here

Codeforces Round #460 (Div. 2) D. Substring BFS拓扑排序dp

Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs

#分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

D. Bananas in a Microwave CodeCraft-21 and Codeforces Round #711 (Div. 2)