Problem Solving Ideas:
The number of numbers containing 49 in 0 to N
Dp[i][0] indicates that the length is I and does not contain 49.
DP[I][1] indicates that the length is I and does not contain 49 and the highest position is 9
DP[I][2] Indicates a length of I and contains 49;
Processing from high to low
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <set># Define LL long long using namespace std; LL dp[25][3];//preprocessing dp array void init () {dp[0][0] = 1;dp[0][1] = dp[0][2] = 0;for (int i=1;i<25;i++) {dp[i][0] = dp[i-1][0] * 10 -Dp[i-1][1];DP [i][1] = dp[i-1][0];DP [i][2] = dp[i-1][2] * ten + dp[i-1][1];}} int bit[25]; ll solve (ll N) {memset (bit,0,sizeof (bit)); LL ans = 0;int len = 0;while (n) {Bit[++len] = n% 10;n/= 10;} BOOL flag = false;for (int i=len;i>=1;i--) {ans + = bit[i] * dp[i-1][2];//current bit can be placed 0 ~ bit[i]-1, total is bit[i] number if (flag) ans + = d P[I-1][0] * bit[i];//If 49 is present, the current bit can be placed Bit[i] number else if (Bit[i] > 4) ans + = dp[i-1][1];//If there is no previous 49, then the current must be 4if (bit[i+1] = = 4 && Bit[i] = = 9) flag = true;} if (flag) ans++;//if the last two bits are 49, plus one, i.e. n itself is the number that satisfies the condition of the return ans; int main () {int t;scanf ("%d", &t), Init (), while (t--) {LL n;scanf ("%i64d", &N);p rintf ("%i64d\n", Solve (n));} return 0;}
HDU 3555 Bomb (digital DP)