题意
问1~n区间内有多少个含有'49'的数?
题解
本题同:HDU ~ 2089 ~ 不要62 (数位DP),我们求出来0~n中不含49的个数cnt,n+1-cnt就是含有49的个数。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 20;
const int INF = 0x3f3f3f3f;
typedef long long LL;
LL n, dp[20];
string s;
LL dfs(int pos, int pre, bool limit)
{
if (pos == -1) return 1;
if (!limit && pre != 4 && dp[pos] != -1) return dp[pos];
LL ans = 0;
int E = limit?s[pos]-'0':9;
for (int i = 0; i <= E; i++)
{
if (pre == 4 && i == 9) continue;
ans += dfs(pos-1, i, limit&&i==E);
}
if (!limit && pre != 4) dp[pos] = ans;
return ans;
}
LL solve(LL x)
{
s = to_string(x); reverse(s.begin(), s.end());
return dfs(s.size()-1, -1, 1);
}
int main()
{
memset(dp, -1, sizeof(dp));
int T; scanf("%d", &T);
while (T--)
{
scanf("%lld", &n);
printf("%lld\n", n+1-solve(n));
}
return 0;
}
/*
3
1
50
500
*/