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
| #include <iostream> #include <cstdio> #include <cstring> #define maxn 10 #define maxm 4600 using namespace std;
int n, m, T;
int f[maxn][maxm], d[maxn], len; int dfs(int pos, bool limit, int sum) { if (!pos) return sum >= 0; if (sum < 0) return 0; if (!limit && ~f[pos][sum]) return f[pos][sum]; int up = limit ? d[pos] : 9, ans = 0; for (int i = 0; i <= up; ++i) ans += dfs(pos - 1, limit && i == d[pos], sum - i * (1 << pos - 1)); if (!limit) f[pos][sum] = ans; return ans; }
int calc(int x) { int s = 0, l = 0; while (x) { ++l; s += x % 10 * 1 << l - 1; x /= 10; } return s; }
int solve(int x) { len = 0; while (x) { d[++len] = x % 10; x /= 10; } return dfs(len, 1, calc(m)); }
int icase; void work() { cin >> m >> n; printf("Case #%d: %d\n", ++icase, solve(n)); }
int main() { cin >> T; memset(f, -1, sizeof f); while (T--) work(); return 0; }
|