[HDOJ4734]F(x)(数位dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734
题意:给定了一个函数,求[0,B]内的数的个数,使得他们换算出来的结果小于F(A)。
数位dp,dp(l,s)表示l位数的值,s为被F(A)减后还剩下的值。如果s<0说明此时的F(x)比F(A)大了,要剪枝掉。直到位数被枚举完全,判断s是否大于等于0。由于这个s最大为9*(∑i=0,9 2^i)≈5120,所以s这一维可以开5120。
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define fr first 4 #define sc second 5 #define cl clear 6 #define BUG puts("here!!!") 7 #define W(a) while(a--) 8 #define pb(a) push_back(a) 9 #define Rint(a) scanf("%d", &a) 10 #define Rll(a) scanf("%I64d", &a) 11 #define Rs(a) scanf("%s", a) 12 #define Cin(a) cin >> a 13 #define FRead() freopen("in", "r", stdin) 14 #define FWrite() freopen("out", "w", stdout) 15 #define Rep(i, len) for(int i = 0; i < (len); i++) 16 #define For(i, a, len) for(int i = (a); i < (len); i++) 17 #define Cls(a) memset((a), 0, sizeof(a)) 18 #define Clr(a, x) memset((a), (x), sizeof(a)) 19 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 20 #define lrt rt << 1 21 #define rrt rt << 1 | 1 22 #define pi 3.14159265359 23 #define RT return 24 #define lowbit(x) x & (-x) 25 #define onecnt(x) __builtin_popcount(x) 26 typedef long long LL; 27 typedef long double LD; 28 typedef unsigned long long ULL; 29 typedef pair<int, int> pii; 30 typedef pair<string, int> psi; 31 typedef pair<LL, LL> pll; 32 typedef map<string, int> msi; 33 typedef vector<int> vi; 34 typedef vector<LL> vl; 35 typedef vector<vl> vvl; 36 typedef vector<bool> vb; 37 38 const int maxn = 10; 39 const int maxm = 5120; 40 int digit[maxn]; 41 int dp[maxn][maxm]; 42 43 int f(int x) { 44 int ret = 0, cnt = 0; 45 while(x) { 46 ret += (x % 10) * (1<<cnt++); 47 x /= 10; 48 } 49 return ret; 50 } 51 52 int dfs(int l, int s, bool flag) { 53 if(l == 0) return s >= 0; 54 if(s < 0) return 0; 55 if(!flag && ~dp[l][s]) return dp[l][s]; 56 int ret = 0; 57 int pos = flag ? digit[l] : 9; 58 Rep(i, pos+1) { 59 ret += dfs(l-1, s-i*(1<<(l-1)), flag&&(i==pos)); 60 } 61 if(!flag) dp[l][s] = ret; 62 return ret; 63 } 64 65 int solve(int x, int y) { 66 int pos = 0; 67 while(x) { 68 digit[++pos] = x % 10; 69 x /=10; 70 } 71 return dfs(pos, y, true); 72 } 73 74 int a, b; 75 76 signed main() { 77 //FRead(); 78 int T, _ = 1; 79 Rint(T); 80 Clr(dp, -1); 81 W(T) { 82 Rint(a); Rint(b); 83 a = f(a); 84 printf("Case #%d: %d\n", _++, solve(b, a)); 85 } 86 RT 0; 87 }