乘法表

题目链接: http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3819&konwledgeId=40

解题思路: 二分答案。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int MAXN = 100005;
 5 typedef long long LL;
 6 
 7 int in(LL x, int n,int m)
 8 {
 9     int p=sqrt(x);
10     for (int i=1;i<=p;++i)
11     {
12         if (x%i==0 && ((i<=n && (int)(x/i)<=m) || (i<=m && (int)(x/i)<=n))) return 1;
13     }
14     return 0;
15 }
16 
17 LL cal(LL x, int n, int m)
18 {
19     LL ans = 0LL;
20     for (int i = 1;i<=m;++i)
21         ans += min((LL)n*i,x)/i;
22     return ans;
23 }
24 
25 LL bisearch(LL l, LL r, LL k, int n, int m)
26 {
27     LL mid;
28     while(l<=r)
29     {
30         mid= l + (r-l)/2;
31         // printf("l=%lld r=%lld mid=%lld\n", l, r, mid);
32         if (cal(mid, n, m)>=k) r=mid-1;
33         else l=mid+1;
34     }
35     LL ans=r+1;
36     while(!in(ans,n,m)) ++ans;
37     return ans;
38 }
39 
40 
41 int main()
42 {
43 #ifndef ONLINE_JUDGE
44     freopen("test.txt","r",stdin);
45 #endif // ONLINE_JUDGE
46     int n,m;
47     LL k;
48     while (cin>>n>>m>>k)
49     {
50         if (n<m) swap(n,m);
51         printf("%lld\n",bisearch(1,(LL)n*m,k,n,m));
52     }
53     return 0;
54 }

 

posted @ 2018-04-21 14:34  只会一点暴力  阅读(140)  评论(0编辑  收藏  举报