数位DP,刚开始学,抄的别人的代码,自己逻辑一定要清晰。
#include "iostream"
using namespace std;
int dp[20][4];
int Dp(int x)
{
bool flag = false;
int s[15];
int idx = 0,summ = x,ans = 0;
for(;x;x /= 10) s[++idx] = x%10;
s[idx + 1] = 0;
for (int i = idx;i > 0;-- i) {
ans += dp[i-1][2]*s[i];
if (flag) ans += dp[i-1][0]*s[i];
else {
if (s[i] > 4) ans += dp[i - 1][0];
if (s[i] > 6) ans += dp[i - 1][1];
if (s[i + 1] == 6 && s[i] > 2 ) ans += dp[i][1];
}
if (s[i]== 4 || s[i+1] == 6&&s[i] == 2)
flag = true;
}
return summ - ans;
}
void Ini()
{
memset(dp,0,sizeof(dp));
dp[0][0] = 1;
for (int i = 1;i < 11;++ i) {
dp[i][0] = dp[i-1][0]*9 - dp[i-1][1];
dp[i][1] = dp[i-1][0];
dp[i][2] = dp[i-1][2]*10 + dp[i-1][1] + dp[i-1][0];
}
}
int main()
{
Ini();
int n, m;
while (cin >> n >> m ) {
if (m + n == 0) break;
cout << Dp(m+1) - Dp(n) << endl;
}
return 0;
}