题意:
问从1~N里有多少个数字含有49这个子串
思路:
同不要62,基础数位DP,代码里注释
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
ll dp[1000][2];
ll a[1000];
ll dfs(int len,bool pre,bool bound )
{
if(!len)
return 1;
if(!bound && dp[len][pre] != -1)
return dp[len][pre];
ll res=0;
int maxx;//判断我们可以取到的最大数字,比如1234我们第二位就不能在第一位是1的情况下取到3
if(bound)
maxx=a[len];
else
maxx=9;
for(int i=0; i<=maxx; i++)
{
if( pre && i == 9)//如果已经是49 就不需要再搜索可以当车牌号的数字了
continue;
res += dfs(len-1,i==4,bound&&i==maxx );//第二个参数传递给下一位,当前是否是6,第三个参数传递给下一位,当前位是否是个边界
}
if(!bound)
dp[len][pre] = res;
return res;
}
//拆数字
ll solve(long long n)
{
ll len = 0;
while(n)
{
a[++len] = n % 10;
n /= 10;
}
return dfs(len,false,true );
}
int main()
{
ll n,m;
memset(dp,-1,sizeof(dp));
int T;
cin>>T;
while(T--)
{
cin>>n;
printf("%lld\n",n-solve(n)+1 );
}
return 0;
}