The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
The input terminates by end of file marker.
3 1 50 500
0
1
15
题目大概:
找出1到n之间的含有连续的49的数的个数。
思路:
基础数位dp。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[30];
long long dp[30][20][15];
long long sove(int pos,int qian,int bb,int limit)
{
if(pos==-1)return bb;
if(!limit&&dp[pos][qian][bb]!=-1)return dp[pos][qian][bb];
long long sum=0;
int end=limit?a[pos]:9;
for(int i=0;i<=end;i++)
{
if(qian==4&&i==9)sum+=sove(pos-1,i,1,limit&&(i==a[pos]));
else sum+=sove(pos-1,i,bb,limit&&(i==a[pos]));
}
if(!limit)dp[pos][qian][bb]=sum;
return sum;
}
long long go(long long x)
{
int pos=0;
while(x)
{
a[pos++]=x%10;
x/=10;
}
return sove(pos-1,0,0,1);
}
int main()
{
int n;
cin>>n;
while(n--)
{ long long m;
memset(dp,-1,sizeof(dp));
scanf("%I64d",&m);
printf("%I64d\n",go(m));
}
return 0;
}