hdu---(3555)Bomb(数位dp(入门))

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7921    Accepted Submission(s): 2778

Problem Description

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?

Input

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.

Output

For each test case, output an integer indicating the final points of the power.

Sample Input

3
1
50
500

Sample Output

0
1
15

Hint

From 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.

Author

[email protected]

Source

2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHU

题意: 给你一个数n,要你统计出1到n中出现含有49数字的个数: 比如 498,549,49.....

对于这一道题: 看到一个博客引用了这张图片,觉得说的很清晰,就引用了..

我们对于 i-1长度的数字分析,无疑就这么集中情况(当然只是围绕49来说的哇)首部分析:

i-1长度                                  那么对于 i长度

首部为49 ,那么它的格式必然为:              49****                                   ?49****(?可能为9)

首部保函9 ,那么它的格式必然为:             9*****                                   ?9*****(?可能为4)

首部部位49 ,那么它的格式为:                *******                                  ?*******(?可能为9)

我们不妨用dp[i][2]表示首部为49的,dp[i][1]表示首部为9的,dp[i][0]表示首部不为49,于是我们可以发现这样一个规律:

dp[i-1][2]向前移一位,即原来的个位变为十位,十位变为百位的那种移位。 形成dp[i][2],但是需要注意的是:

当dp[i-1][2]时,其实由我上面说的,?可能为9 ,所以当向前移一位时,?为9的可能性被去掉了。所以

dp[i-1][2]*10(移动一位时)需要减去 开头为9的那种模式dp[i-1][1],所以得到:

(1)      dp[i][2]=dp[i-1][2]*10-dp[i-1][1];

对于i位首部为9那么后面只需要满足不为49即可,刚好满足dp[i][0];

(2)  所以 dp[i][1]=d[i-1][0];

对于首部不为49的

同样也可以分析出来...

dp[i][0]=dp[i-1][0]*10+dp[i-1][1];

于是得到这样一个预处理方程:

dp[i][2]=dp[i-1][2]*10-dp[i-1][1];

dp[i][1]=d[i-1][0];

dp[i][0]=dp[i-1][0]*10+dp[i-1][1];

代码:详情见代码:

 1 //#define LOCAL
 2 #include<cstdio>
 3 #include<cstring>
 4 #define LL __int64
 5 using namespace std;
 6  const int maxn=25;
 7 LL dp[maxn][3]={0};
 8 int nn[maxn];
 9 int main()
10 {
11
12   #ifdef LOCAL
13     freopen("test.in","r",stdin);
14   #endif
15  int cas,i;
16  LL n;
17  scanf("%d",&cas);
18  /*数位DP的惯有模式预处理*/
19  dp[0][0]=1;
20  for(i=1;i<=20;i++)
21  {
22     dp[i][0]=dp[i-1][0]*10-dp[i-1][1];
23     dp[i][1]=dp[i-1][0];
24     dp[i][2]=dp[i-1][2]*10+dp[i-1][1];
25  }
26  while(cas--)
27  {
28    scanf("%I64d",&n);
29    i=0;
30    n+=1;
31    memset(nn,0,sizeof(nn));
32    while(n>0)
33    {
34      nn[++i]=n%10;
35      n/=10;
36    }
37    LL ans=0;
38    bool tag=0;
39    int num=0;
40    for(  ; i>=1  ; i--  )
41    {
42          ans+=dp[i-1][2]*nn[i];  /*计算49开头的个数*/
43          if(tag){
44         ans+=dp[i-1][0]*nn[i];   /*当前面出现了49的时候,那么后面出现的任何数字也要进行统计*/
45       }
46       if(!tag&&nn[i]>4)
47       {
48           ans+=dp[i-1][1];      /*如果没有出现49开头,只要首部大于5,那么必定保函有一个49*/
49       }
50       if(num==4&&nn[i]==9)
51              tag=1;
52       num=nn[i];
53    }
54     printf("%I64d\n",ans);
55  }
56  return 0;
57 }

时间: 2024-12-28 19:29:25

hdu---(3555)Bomb(数位dp(入门))的相关文章

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

hdu 3555(数位dp 入门)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7716    Accepted Submission(s): 2702 Problem Description The counter-terrorists found a

HDU 3555 Bomb (数位DP)

数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下来. 简介: 顾名思义,所谓的数位DP就是按照数字的个,十,百,千--位数进行的DP.数位DP的题目有着非常明显的性质: 询问[l,r]的区间内,有多少的数字满足某个性质 做法根据前缀和的思想,求出[0,l-1]和[0,r]中满足性质的数的个数,然后相减即可. 算法核心: 关于数位DP,貌似写法还是

HDU 3555 Bomb(数位DP)

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

HDU(3555),数位DP

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 15372    Accepted Submission(s): 5563 Problem Description The counter-terrorists f

hud 3555 Bomb 数位dp

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 19122    Accepted Submission(s): 7068 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorist

HDU 3555 Bomb [数位]

Description 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

hdu3555 Bomb (数位dp入门题)

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 19698    Accepted Submission(s): 7311 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists

数位DP入门题 hdu 2089 hdu 3555

hdu 2089 不要62 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:对于每次给出的一个区间,找出区间内多少个数字没有出现62和4. 思路: 数位DP入门题,对于dfs设置3个参数. 一个表示现在进行到第几位, 一个表示前一个标记状态,这里表示上一位是不是6. 一个表示是否现在是这位可以取到的最大的数字. 如果上一位是6,那么这位不可以取2.且当前位都不可以取4. 1 #include <bits/stdc++.h> 2 us

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&