您的位置:首页 > 其它

hdu 3555 Bomb(数位dp)

2014-08-16 19:20 453 查看

Bomb

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 7487 Accepted Submission(s): 2610



[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.  题意:给出n,求出[1,n]中多少个数包含49;思路:数位dp。1.dp[len][0] 代表数字长度为len不含49的个数 2.dp[len][1] 代表数字长度为len不含49但是以9开头的个数(显然dp[len][1]包含在dp[len][0]中)3.dp[len][2] 代表数字长度为len含有49的个数 AC代码:[code]#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;

const int maxn = 3005;
const int INF = 1e9;
const int mask = 0x7fff;

ll dp[25][3];
ll digit[25];
void init(){
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for(int i = 1; i < 21; i++)
{
dp[i][0] = 10 * dp[i - 1][0] - dp[i - 1][1];   //在前面添上0~9,要减去4加到9前面这种情况
dp[i][1] = dp[i - 1][0];
dp[i][2] = 10 * dp[i - 1][2] + dp[i - 1][1];
}
}
int main()
{
init();
int t;
ll n;
scanf("%d", &t);
while(t--)
{
scanf("%I64d", &n);
int len = 1;
memset(digit, 0, sizeof(digit));
while(n)
{
digit[len++] = n % 10;
n /= 10;
}
ll ans = 0;
bool flag = 0;
for(int i = len - 1; i >= 1; i--)
{
ans += dp[i - 1][2] * digit[i];
if(flag) ans += digit[i] * dp[i - 1][0];
else if(digit[i] > 4) ans += dp[i - 1][1];
if(digit[i + 1] == 4 && digit[i] == 9) flag = 1;
}
if(flag) ans++;
printf("%I64d\n", ans);
}
return 0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: