您的位置:首页 > 其它

HDU3555 Bomb(数位dp)

2016-01-26 18:52 363 查看
[align=left]Problem Description[/align]
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?

[align=left]Input[/align]
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.

[align=left]Output[/align]
For each test case, output an integer indicating the final points of the power.

[align=left]Sample Input[/align]

3
1
50
500


[align=left]Sample Output[/align]

0
1
15

HintFrom 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499",
so the answer is 15.


HDU2089一样的思想,我发现网上的题解大多是

dp[i][0]表示不含有49

dp[i][1]表示不含有49,且最高位为9

dp[i][2]表示含有49

这样的方法做的,改天试一下吧。

#include<iostream>
#include<cstdio>
using namespace std;
long long dp[20][10];
void Init()
{
dp[0][0]=1;
for(int i=1;i<=20;i++)
for(int j=0;j<=9;j++)//枚举第i位
for(int k=0;k<=9;k++)//枚举第i-1位
if(!(j==4&&k==9))
dp[i][j]+=dp[i-1][k];
}
long long solve(long long n)
{
long long ans=0;
int digit[20];//表示n从右到左第i位的数字
int len=0;
while(n>0)
{
digit[++len]=n%10;
n/=10;
}
digit[len+1]=0;
for(int i=len;i>=1;i--)//枚举第i位
{
for(int j=0;j<digit[i];j++)//枚举第i位的数字
if(!(digit[i+1]==4&&j==9))
ans+=dp[i][j];
if((digit[i+1]==4&&digit[i]==9))
break;
}
return ans;
}
int main(void)
{
int t;
long long n;
Init();
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
printf("%lld\n",n+1-solve(n+1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: