#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 }
时间: 2025-01-01 08:11:38

#256 (Div. 2)D. Multiplication Table的相关文章

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

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/contest/448/problem/D -----------------------------------------------------------------------------------------------------------------------------------------------

Codeforces Round #256 (Div. 2) D. 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 an n?×?m multiplication table, where the element on the i

Codeforces Round #256 (Div. 2) Multiplication Table

刚刚开始想到一种很暴力但还行的方法,不过在和TK讨论的过程中引出了一个更好的算法 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; long long n,m,k; long long check(long long x) { long long ans=0; for(int i=1;i<=n;i++) { ans

Codeforces Round #256 (Div. 2) A/B/C/D

A. Rewards 水题 #include<cstdio> #include<iostream> #include<cstring> using namespace std; int main() { int a1,a2,a3,b1,b2,b3,s,t1,t2,sum1,sum2; while(scanf("%d%d%d",&a1,&a2,&a3)!=EOF) { scanf("%d%d%d",&

Codeforces Round #256 (Div. 2)D 二分答案

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 tabl

Codeforces #256 Div.2

B. Suffix Structure 1. 先判断s去掉一些元素是否能构成t,如果可以就是automaton 判断的方法也很简单,two pointer,相同元素同时++,不相同s的指针++,如果t能全找到,那么s能够去掉元素构成t. bool f(string s, string t) { int i = 0, j = 0; while (i < s.size() && j < t.size()) { if (s[i] == t[j]) { i++; j++; } else

Codeforces Round #256 (Div. 2) 题解

Problem A: A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Bizon the Champion is called the Champion for a reason. Bizon the Champion has recently got a present - a new glass cup

HDOJ 4915 Multiplication table

可以特判出0和1,再统计每行出现了多少种十位上的数就可以了.... 没有考虑2的情况似乎也过了...... Multiplication table Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 800    Accepted Submission(s): 360 Problem Description Teacher Mai has

HDU4951:Multiplication table

Problem Description Teacher Mai has a multiplication table in base p. For example, the following is a multiplication table in base 4: * 0 1 2 3 0 00 00 00 00 1 00 01 02 03 2 00 02 10 12 3 00 03 12 21 But a naughty kid maps numbers 0..p-1 into another