UVa 714 Copying Books - 二分答案

 


  求使最大值最小,可以想到二分答案。

  然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难)。

Code

 1 /**
 2  * UVa
 3  * Problem#12627
 4  * Accepted
 5  * Time:0ms
 6  */
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<cctype>
10 #include<ctime>
11 #include<cstring>
12 #include<cstdlib>
13 #include<fstream>
14 #include<sstream>
15 #include<algorithm>
16 #include<map>
17 #include<set>
18 #include<stack>
19 #include<queue>
20 #include<vector>
21 #include<stack>
22 #ifndef WIN32
23 #define AUTO "%lld"
24 #else
25 #define AUTO "%I64d"
26 #endif
27 using namespace std;
28 typedef bool boolean;
29 #define inf 0xfffffff
30 #define smin(a, b) a = min(a, b)
31 #define smax(a, b) a = max(a, b)
32 template<typename T>
33 inline void readInteger(T& u){
34     char x;
35     int aFlag = 1;
36     while(!isdigit((x = getchar())) && x != '-');
37     if(x == '-'){
38         x = getchar();
39         aFlag = -1;
40     }
41     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
42     ungetc(x, stdin);
43     u *= aFlag;
44 }
45 
46 int T;
47 int n, a, b;
48 
49 template<typename T>
50 T pow(T a, int pos) {
51     if(pos == 0)    return 1;
52     if(pos == 1)    return a;
53     T temp = pow(a, pos / 2);
54     if(pos & 1)        return temp * temp * a;
55     return temp * temp;
56 }
57 
58 inline void init() {
59     readInteger(n);
60     readInteger(a);
61     readInteger(b);
62 }
63 
64 long long dfs(int dep, int top, int bottom) {
65     if(dep == 0)    return 1;
66     if(top == 1 && bottom == (1 << dep))    return pow((long long)3, dep);
67     int mid = 1 << (dep - 1);
68     if(bottom <= mid)    return 2 * dfs(dep - 1, top, bottom);
69     if(top > mid)    return dfs(dep - 1, top - mid, bottom - mid);
70     return 2 * dfs(dep - 1, top, mid) + dfs(dep - 1, 1, bottom - mid);
71 }
72 
73 inline void solve() {
74     long long res = dfs(n, a, b);
75     printf(AUTO"\n", res);
76 }
77 
78 int main() {
79     readInteger(T);
80     for(int kase = 1; kase <= T; kase++) {
81         init();
82         printf("Case %d: ", kase);
83         solve();
84     }
85     return 0;
86 }
posted @ 2017-04-03 11:58  阿波罗2003  阅读(223)  评论(0编辑  收藏  举报