题意:F(x) = An * 2n-1 + An-1 * 2n-2 + … + A2 * 2 + A1 * 1,Ai是十进制数位,然后给出a,b求区间[0,b]内满足F(i)<=F(a)的i的个数
题解:数位dp
d
p
[
p
o
s
]
[
s
t
a
]
dp[pos][sta]
dp[pos][sta]:当前哪一位,之后的和为sta的数量。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
int t, n, b, fa, a[9], dp[9][10005];
int f(int x) {
if (x == 0) return 0;
return f(x / 10) * 2 + x % 10;
}
int dfs(int pos, int sta, int limit) {
if (pos == -1) return sta <= fa;
if (!limit && dp[pos][fa - sta] != -1) return dp[pos][fa - sta];
int up = limit ? a[pos] : 9;
int ans = 0;
for (int i = 0; i <= up; i++) {
if (sta + i * (1 << pos) > fa) continue;
ans += dfs(pos - 1, sta + i * (1 << pos), limit && i == a[pos]);
}
if (!limit) dp[pos][fa - sta] = ans;
return ans;
}
int solve(int x) {
int pos = 0;
while (x) {
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos - 1, 0, 1);
}
int main() {
int cas = 0;
scanf("%d", &t);
memset(dp, -1, sizeof(dp));
while (t--) {
scanf("%d%d", &n, &b);
fa = f(n);
printf("Case #%d: %d\n", ++cas, solve(b));
}
return 0;
}