You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1 KiB

2 years ago
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 32;
const int M = 1024 * 10 + 10;
int f[N][M];
int a[N];
int fa;
int F(int x) {
if (x == 0) return 0;
return F(x / 10) * 2 + x % 10;
}
int dfs(int pos, int sum, bool limit) {
if (pos == 0) return sum <= fa;
if (sum > fa) return 0;
if (!limit && ~f[pos][sum]) return f[pos][sum];
int ans = 0;
int up = limit ? a[pos] : 9;
for (int i = 0; i <= up; i++)
ans += dfs(pos - 1, sum + i * (1 << (pos - 1)), limit && i == a[pos]);
if (!limit) f[pos][sum] = ans;
return ans;
}
inline int calc(int x) {
memset(f, -1, sizeof f);
int al = 0;
while (x) a[++al] = x % 10, x /= 10;
return dfs(al, 0, true);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
int cnt = 1;
cin >> T;
while (T--) {
int x, y;
cin >> x >> y;
fa = F(x);
printf("Case #%d: %d\n", cnt++, calc(y));
}
return 0;
}