Bomb
Title: http://acm.hdu.edu.cn/showproblem.php?pid=3555
Q: Give you a number n (1 <= n <= 2^63-1) and ask how many times the "49" sequence appears in 1~n these numbers.
Solving: Digital DP. It's actually a DP with a number of units, something like a dictionary tree in an AC automaton.
We can divide the situation into 3 kinds according to the dictionary tree, "49" in the sequence, no "49" in the sequence, but the last one is "4", there is no "49" in the sequence and the last one is not "4".
In these 3 states of the transfer can be.
Dp[k][0] = dp[k-1][1] * 8 + dp[k-1][0] * 9; 0 does not contain 49 and ends with a number other than 4
DP[K][1] = dp[k-1][0] + dp[k-1][1]; 1 does not contain 49 and ends with the number 4
DP[K][2] = dp[k-1][2] * + dp[k-1][1]; 2 with 49 status
Code:
#include <cstdio> #include <cstring> using namespace std;
#define LL Long-long char s[25];
LL dp[25][3][2];
/* Dp[k][0] = dp[k-1][1] * 8 + dp[k-1][0] * 9;
DP[K][1] = dp[k-1][0] + dp[k-1][1];
DP[K][2] = dp[k-1][2] * + dp[k-1][1]; 0 does not contain 49 and ends with a number other than 4 1 does not contain 49 and 4 is the end of the number 2 with 49 status */int next (int a,int b) {if a==2| | (a==1&&b==9))
return 2;
else return b==4;
int main () {int t;
scanf ("%d", &t);
for (; t--;)
{scanf ("%s", s+1);
Memset (Dp,0,sizeof (DP));
int Len=strlen (s+1);
Dp[0][0][1]=1;
for (int i=1; i<=len; ++i) {Dp[i][0][0] = dp[i-1][0][0]*9 + dp[i-1][1][0]*8;
Dp[i][1][0] = dp[i-1][0][0] + dp[i-1][1][0];
Dp[i][2][0] = dp[i-1][2][0] * + dp[i-1][1][0];
for (int j=0; j<s[i]-' 0 '; ++j) {Dp[i][next (0,j)][0]+=dp[i-1][0][1];
Dp[i][next (1,J)][0]+=dp[i-1][1][1]; Dp[i][next (2,J)][0]+=dP[I-1][2][1];
} dp[i][next (0,s[i]-' 0 ')][1]+=dp[i-1][0][1];
Dp[i][next (1,s[i]-' 0 ')][1]+=dp[i-1][1][1];
Dp[i][next (2,s[i]-' 0 ')][1]+=dp[i-1][2][1];
printf ("%i64d\n", dp[len][2][0]+dp[len][2][1]);
}
}
Source: http://blog.csdn.net/ACM_Ted