题目链接
这个题算是个入门题,注意拿pre6记录一下上一次是否去了6即可
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int b[21];
int dp[21][2];
int dfs(int pos, int preok, int pre6)
{if(pos == -1) return 1;if(preok && dp[pos][pre6] != -1) return dp[pos][pre6];int up = preok ? 9 : b[pos]; int sum=0;for(int i = 0;i <= up; i ++ ){if(i != 4 && !(pre6 && i == 2)){sum += dfs(pos - 1, preok || i < b[pos], i == 6);}}if(preok) dp[pos][pre6] = sum;return sum;
}
int solve(int r)
{int cnt = 0;while(r){b[cnt++] = r % 10;r /= 10;}return dfs( -- cnt, 0, 0);
}
int main()
{memset(dp, - 1, sizeof(dp));int n, m;while(~scanf("%d %d", &n, &m) && (n || m)){cout << solve(m) - solve(n - 1) << endl;}
}