forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
126 lines (107 loc) · 3.02 KB
/
Copy pathB.cpp
File metadata and controls
126 lines (107 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <map>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
deque<int> ta, tb, tk;
LL a, b, k, n;
LL mm[35][2][2][2];
/*LL dp(int idx, int fa, int fb, int fk) {
LL& res = mm[idx][fa][fb][fk];
if (res != -1) return res;
if (idx == n) return res = 1;
if (fa + fb + fk == 0) {
res = 1LL;
REP(i,n-idx) res *= 4LL;
return res;
}
int ma = ta[idx], mb = tb[idx], mk = tk[idx];
if (fa == 0) ma = 1;
if (fb == 0) mb = 1;
res = 0;
FOR(i,0,ma) {
FOR(j,0,mb) {
int t = (i & j);
if ((fk == 1) && (t > mk)) continue;
int na = fa, nb = fb, nk = fk;
if (i < ma) na = 0;
if (j < mb) nb = 0;
if (t < mk) nk = 0;
res += dp(idx + 1, na, nb, nk);
}
}
return res;
}*/
void run() {
LL A, B, K;
cin >> A >> B >> K;
if (A < K || B < K) {
LL res = A * B;
cout << res << endl;
return;
}
a = A - 1, b = B - 1, k = K - 1;
ta.clear(), tb.clear(), tk.clear();
while (true) {
ta.push_front(a & 1);
tb.push_front(b & 1);
tk.push_front(k & 1);
if (a + b + k == 0) break;
a >>= 1, b >>= 1, k >>= 1;
}
n = ta.size();
memset(mm, -1, sizeof(mm));
RFOR(idx,n,0) {
REP(fa,2) {
REP(fb,2) {
REP(fk,2) {
LL& res = mm[idx][fa][fb][fk];
if (idx == n) {
res = 1;
continue;
}
if (fa + fb + fk == 0) {
res = 1LL;
REP(i,n-idx) res *= 4LL;
continue;
}
int ma = ta[idx], mb = tb[idx], mk = tk[idx];
if (fa == 0) ma = 1;
if (fb == 0) mb = 1;
res = 0;
FOR(i,0,ma) {
FOR(j,0,mb) {
int t = (i & j);
if ((fk == 1) && (t > mk)) continue;
int na = fa, nb = fb, nk = fk;
if (i < ma) na = 0;
if (j < mb) nb = 0;
if (t < mk) nk = 0;
res += mm[idx + 1][na][nb][nk];
}
}
}
}
}
}
cout << mm[0][1][1][1] << endl;
/*LL res = dp(0, 1, 1, 1);
cout << res << endl;*/
}
int main() {
int nk;
cin >> nk;
FOR(c,1,nk) {
cout << "Case #" << c << ": ";
run();
}
return 0;
}