HDU 4249 A Famous Equation (Digital DP)
Train of Thought: use d [I] [a] [B] [c] [is] to represent the current I-bit. The I-bits of the three numbers are a, B, c, whether there is an increment, the number of methods.
For details, see the code:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define Max (a, B) (a)> (B )? (A) :( B) # define Min (a, B) (a) <(B )? (A) :( B) using namespace std; typedef long ll; typedef long double ld; const ld eps = 1e-9, PI = 3.1415926535897932384626433832795; const int mod = 1000000000 + 7; const int INF = 0x3f3f3f; // & 0x7FFFFFFFconst int seed = 131; const ll INF64 = ll (1e18); const int maxn = 15; int T, n, m, len, vis [maxn] [maxn] [maxn] [maxn] [2], len1, len2, len3, kase = 0; char a [maxn], B [maxn], c [maxn], s [100]; ll d [maxn] [maxn] [maxn] [m Axn] [2]; ll dp (int pos, int bb, int cc, int dd, int is) {ll & ans = d [pos] [bb] [cc] [dd] [is]; if (pos> len) return is = 0; if (vis [pos] [bb] [cc] [dd] [is] = kase) return ans; vis [pos] [bb] [cc] [dd] [is] = kase; ans = 0; if (a [pos] = '? '& B [pos] = '? ') {For (int I = 0; I <10; I ++) {for (int j = 0; j <10; j ++) {if (pos = len1 & I = 0 & len1! = 1) continue; if (pos = len2 & j = 0 & len2! = 1) continue; int cur = I + j + is; int res = 0; if (cur> = 10) {cur-= 10; ++ res ;} if (c [pos] = '? '&&! (Pos = len3 & cur = 0 & len3! = 1) ans + = dp (pos + 1, I, j, cur, res); else if (c [pos]-'0' = cur) ans + = dp (pos + 1, I, j, cur, res) ;}} else if (a [pos] = '? ') {For (int I = 0; I <10; I ++) {if (pos = len1 & I = 0 & len1! = 1) continue; int cur = I + B [pos]-'0' + is; int res = 0; if (cur> = 10) {cur-= 10; ++ res;} if (c [pos] = '? '&&! (Pos = len3 & cur = 0 & len3! = 1) ans + = dp (pos + 1, I, B [pos]-'0', cur, res ); else if (c [pos]-'0' = cur) ans + = dp (pos + 1, I, B [pos]-'0', cur, res) ;}} else if (B [pos] = '? ') {For (int I = 0; I <10; I ++) {if (pos = len2 & I = 0 & len2! = 1) continue; int cur = I + a [pos]-'0' + is; int res = 0; if (cur> = 10) {cur-= 10; ++ res;} if (c [pos] = '? '&&! (Pos = len3 & cur = 0 & len3! = 1) ans + = dp (pos + 1, a [pos]-'0', I, cur, res ); else if (c [pos]-'0' = cur) ans + = dp (pos + 1, a [pos]-'0', I, cur, res) ;}} else {int cur = a [pos]-'0' + B [pos]-'0' + is; int res = 0; if (cur> = 10) {cur-= 10; ++ res;} if (c [pos] = '? '&&! (Pos = len3 & cur = 0 & len3! = 1) ans + = dp (pos + 1, a [pos]-'0', B [pos]-'0', cur, res ); else if (c [pos]-'0' = cur) ans + = dp (pos + 1, a [pos]-'0 ', B [pos]-'0', cur, res);} return ans;} int main () {while (~ Scanf ("% s", s + 1) {len = strlen (s + 1); len1 = 0; len2 = 0; len3 = 0; int id = 0; for (int I = len; I> = 1; I --) {if (s [I] = '| s [I] =' + ') {id ++; continue;} if (id = 0) {c [++ len3] = s [I];} else if (id = 1) {B [++ len2] = s [I];} else {a [++ len1] = s [I] ;}} len = max (len1, max (len2, len3); // incomplete completion, reduce the amount of code for (int I = len1 + 1; I <= len; I ++) a [I] = '0 '; for (int I = len2 + 1; I <= len; I ++) B [I] = '0'; for (int I = len3 + 1; I <= len; I ++) c [I] = '0'; ++ kase; ll ans = dp (1, 0, 0, 0, 0 ); printf ("Case % d: % I64d \ n", kase, ans);} return 0 ;}