-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhdu4734.cpp
More file actions
83 lines (73 loc) · 1.65 KB
/
hdu4734.cpp
File metadata and controls
83 lines (73 loc) · 1.65 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
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cmath>
int dp[10][2000];
int num[10];
int w;
int accum;
int ten[10];
int dodp(int len, int cw, int an, bool limitR) {
if(len == 0) return cw <= w;
if(cw > w) return 0;
if(!limitR && dp[len][cw] != -1)
return dp[len][cw];
int limit = limitR?num[len]:9;
int ans = 0;
int next_an = an>>1;
for(int i = 0; i <= limit; ++i) {
int tmp = cw + i*an;
if(tmp > w) continue;
if(!limitR && tmp+9*(an-1)<= w) {
// max weight when n = 1, 2, 3, 4, 5 = 9 ,27, 63, 135
// = 9 * (2^0 + 2^1 + ... 2^n)
// = 9 * (2^(n+1) - 1)
ans += ten[len - 1];
} else if (tmp == w) {
ans++;
} else {
ans += dodp(len-1, tmp, next_an, limitR&&i==limit);
}
}
if(!limitR) dp[len][cw] = ans;
return ans;
}
void calc_weight(int n) {
int ac = 1;
w = 0;
while(n > 0) {
w += ac * (n % 10);
n /= 10;
ac <<= 1;
}
}
int solve(int n) {
int len = 1;
accum = 1;
while(n > 0) {
num[len++] = n % 10;
n /= 10;
accum <<= 1;
}
accum >>= 1;
len--;
// printf("len: [%d] accum: [%intd] w: [%intd]\n", len, accum, w);
return dodp(len, 0, accum, true);
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int T;
int a, b;
// clock_t begin_time = clock();
ten[0] = 1;
for(int i = 1; i < 10; ++i) ten[i] = ten[i-1]*10;
scanf("%d", &T);
for(int tc = 1; tc <= T; ++tc) {
memset(dp, -1, sizeof(dp));
scanf("%d%d", &a, &b);
calc_weight(a);
printf("Case #%d: %d\n", tc, solve(b));
}
// printf("%lf\n", float( clock () - begin_time ));
}