/* Digit DP meaning: Find the number solution that contains the substring 13 between 1-N and can be divisible by 13: at the beginning, DP [N] [N] [2] here 2 is used to record whether the current BIT is 13, I have not recorded the case where the last bit is 1 and the current BIT is 13 and the last bit is 1 in the array. */# Include <stdio. h> # include <string. h> # define n 14int DP [N] [N] [3]; int digit [N]; int DFS (INT Len, int mod, int CNT, int OK) {If (! Len) {If (mod = 0 & CNT = 2) return 1; return 0;} If (! OK & DP [Len] [mod] [CNT]! =-1) return DP [Len] [mod] [CNT]; int ans = 0, I, Maxx = OK? Digit [Len]: 9; for (I = 0; I <= Maxx; I ++) {If (CNT = 2 | (CNT = 1 & I = 3) ans + = DFS (len-1, (mod * 10 + I) % 13,2, OK & I = Maxx); else if (I = 1) // At the beginning, the judgment condition is written as if (CNT = 0 & I = 1) this is not true because of missing a situation (I = 1 & CNT = 1) ans + = DFS (len-1, (mod * 10 + I) % 13, 1, OK & I = Maxx); else ans + = DFS (len-1, (mod * 10 + I) % 13,0, OK & I = Maxx);} If (! OK) DP [Len] [mod] [CNT] = ans; return ans;} int F (INT N) {int Len = 0; while (N) {digit [++ Len] = n % 10; N/= 10;} return DFS (Len, 0, 0, 1) ;}int main () {int N; memset (DP,-1, sizeof (DP); While (scanf ("% d", & N )! = EOF) {printf ("% d \ n", F (N) ;}return 0 ;}
<PRE name = "code" class = "CPP">/* in my original thought, I did not enable one-dimensional record pre at the beginning */# include <stdio. h> # include <string. h> # define n 14int DP [N] [N] [2] [2]; int digit [N]; int DFS (INT Len, int mod, int pre, int CNT, int OK) {If (! Len) {If (mod = 0 & CNT = 1) return 1; return 0;} If (! OK & DP [Len] [mod] [pre] [CNT]! =-1) return DP [Len] [mod] [pre] [CNT]; int ans = 0, I, Maxx = OK? Digit [Len]: 9; for (I = 0; I <= Maxx; I ++) {If (CNT | (pre & I = 3 )) ans + = DFS (len-1, (mod * 10 + I) % 13, I =, OK & I = Maxx); else ans + = DFS (len-1, (mod * 10 + I) % 13, I =, OK & I = Maxx);} If (! OK) DP [Len] [mod] [pre] [CNT] = ans; return ans;} int F (INT N) {int Len = 0; while (N) {digit [++ Len] = n % 10; N/= 10;} return DFS (Len,);} int main () {int N; memset (DP,-1, sizeof (DP); While (scanf ("% d", & N )! = EOF) {printf ("% d \ n", F (N) ;}return 0 ;}
HDU 3652 digital DP