版权声明:欢迎转载,若转载,请标明出处,如有错误,请指点,也欢迎大佬们给出优化方法 https://blog.csdn.net/Charles_Zaqdt/article/details/86532302
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555
题意是输入一个n,求出1-n中有多少个数不包含49。
裸的数位dp。
AC代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[20];
ll dp[20][10];
ll dfs(int pos, int pre, bool limit){
if(pos == -1) return 1;
if(!limit && dp[pos][pre] != -1) return dp[pos][pre];
int up = limit ? a[pos] : 9;
ll ans = 0;
for(int i=0;i<=up;i++){
if(pre == 4 && i == 9) continue;
ans += dfs(pos - 1, i, limit && i == a[pos]);
}
return limit ? ans : dp[pos][pre] = ans;
}
ll solve(ll x){
int pos = 0;
while(x){
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos - 1, 0, true);
}
int main()
{
int T;
memset(dp, -1, sizeof(dp));
scanf("%d",&T);
while(T--){
ll n;
scanf("%lld",&n);
printf("%lld\n",n - solve(n) + 1);
}
return 0;
}