Hdu 4734 F (x) digital dp, hdu4734
Definition F (x) =N * 2N-1 +N-1 * 2N-2 +... +2*2 +1*1 (x =)NAN-1AN-2...2A1), given A, B, and the I in the [0, B] range, F (I) <= F ()
.
Idea: Set dp [pos] [k] as the current pos bit, and then (pos + 1) the number of F function values formed by the combination of BITs and the previous bits cannot exceed k. For details, see the code:
/*************************************** * ****************** File name: hdu4734.cpp author: kereo create time: ******************************** * ***********************/# include <iostream> # include <cstdio> # include <cstring> # include <queue> # include <set> # include <map> # include <vector> # include <stack> # include <cmath> # include <string> using namespace std; typedef long ll; const int sig Ma_size = 26; const int N = 10; const int MAXN = 6000 + 50; const int inf = 0x3fffffff; const double eps = 1e-8; const int mod = 100000000 + 7; # define L (x) (x <1) # define R (x) (x <1 | 1) # define PII pair <int, int> # define mk (x, y) make_pair (x), (y) int res; int bit [N], dp [N] [MAXN], a [N], p [N]; int dfs (int pos, int st, int flag) {if (pos =-1) return 1; if (flag & dp [pos] [st]! =-1) return dp [pos] [st]; int ans = 0; int x = flag? 9: bit [pos]; for (int I = 0; I <= x; I ++) {int k = st-(int) p [pos] * I; if (k <0) continue; ans + = dfs (pos-1, k, flag | I <x);} if (flag) dp [pos] [st] = ans; return ans;} int solve (int x) {int len = 0; do {bit [len ++] = x % 10; x/= 10;} while (x ); return dfs (len-1, res, 0);} int main () {int T, kase = 0; scanf ("% d", & T ); p [0] = 1; for (int I = 1; I <10; I ++) p [I] = p [I-1] * 2; memset (dp, -1, sizeof (dp); while (T --) {int A, n; scanf ("% d", & A, & n ); int cnt = 0; res = 0; do {a [cnt ++] = A % 10; A/= 10; res + = p [cnt-1] * a [cnt-1];} while (A); int ans = solve (n); printf ("Case # % d: % d \ n ", ++ kase, ans);} return 0 ;}