Processing math: 100%

HDU 3555 Bomb 【数位DP】

tonyfang posted @ 2015年8月21日 17:56 in HDU with tags c++ HDU , 627 阅读

HDU 3555 Bomb【数位DP】

数位DP第三题。学习ing~

题目传送门:这里

题目大意:

[1,x]中含有49的数的个数。T次询问,1T100001x2631

前面讲的貌似是第一种数位DP的方法啦,现在我们来讲第二种。

这种一般用三个dp数组来表达,好像叫“有穷自动机”(雾?)好高端。

本题的话,f[i,0]表示长度小于等于i且不含49的数的个数f[i,1]表示长度等于i且最高位含有9的不含49的数的个数f[i,2]表示长度小于等于i49的数的个数。

听起来十分的饶舌,但是很好写转移方程。

对于f[i,0],长度小于等于i不含49的个数:那么就把长度小于等于i1不含49的个数乘10就好了吧!(捂脸),不对,还要减去长度等于i1最高位含有9的不含49的个数。所以f[i,0]=f[i1,0]×10f[i1,1]

对于f[i,1],长度等于i的最高位为9的不含49的个数,那么就是长度小于等于i1的不含49的个数,第i位填个9就好啦!好有道理,那么f[i,1]=f[i1,0]

对于f[i,2]表示长度小于等于i49的数的个数,等于长度小于等于i1的含49的个数,最高位随便填都行,完了吗?没有!还要加上长度为i1最高位为9的不含49的个数,因为填一个4就造出49了!所以,f[i,2]=f[i1,2]×10+f[i1,1]

好啦,dp预处理好了,下面就是正常的处理程序啦。

关于状态的设计一般有3个方面:

f[i,0]表示长度小于等于i且不符合条件的数的个数f[i,1]表示长度等于i且(不)符合条件(+特殊限制)的的数的个数f[i,2]表示长度小于等于i符合条件的数的个数。

特殊限制一般就有几个方面,比如本题要49,特殊限制就是最高位是9;因为这样设计,下一个填4就会造出49,好进行转移。上一题数位dp:不要62好像也可以这样来做呀,诸君稍等,容我打打代码。

2015/08/21/18:30已更新,下一篇博文啦。

 

hdu3555
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# include <stdio.h>
# include <string.h>
using namespace std;
 
typedef unsigned long long ull;
 
ull dp[25][3];
int T;
//dp[i][0]:长度<=i的不含49的数的个数 
//dp[i][1]:长度为i,且最高位含有9的不含49的数的个数 
//dp[i][2]:长度<=i的含有49的数个数
inline void getdp() {
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for (int i=1;i<=22;++i) {
        dp[i][0]=dp[i-1][0]*10-dp[i-1][1];
        dp[i][1]=dp[i-1][0];
        dp[i][2]=dp[i-1][2]*10+dp[i-1][1];
    }
}
 
int d[25],dn;
inline void getd(ull x) {
    dn=0;memset(d,0,sizeof(d));
    while(x) d[++dn]=x%10,x/=10;
}
 
ull calc(ull x) {
    ull cnt=0; bool flag=0;
    for (int i=dn;i>=1;--i) {
        cnt+=dp[i-1][2]*d[i];
        if(flag) cnt+=dp[i-1][0]*d[i];
        else {
            if(d[i]>4) cnt+=dp[i-1][1];
            if(d[i+1]==4&&d[i]==9) flag=1;
        }
    }
    if(flag) cnt++;
    return cnt;
}
 
int main() {
    ull x;
    scanf("%d",&T);
    getdp();
    while(T--) {
        scanf("%I64u",&x);
        getd(x);
        printf("%I64u\n",calc(x));
    }
    return 0;
}
Grammarly Alternativ 说:
2022年8月10日 17:08

Well, to be precise it is very important for anything to run properly such as a business creative which is grammar perfect or else a news outlet or a blog article which is properly checked for any grammar mistakes. Grammarly Alternative At the same time, people love to believe that working knowledge of grammar better resides with humans but it is true that the modern softwares, tools and plugins like Grammarly has been a great help to anyone who is looking to ensure that their words make sense.

12thmodelpapers.in 说:
2023年4月18日 18:37

Board 12th Class Model Paper 2023 Aspirants who had Registered for the Board 12th Class Exam can Download the Previous Paper When Board 12th Announces the Dates to Download the Question Paper. 12thmodelpapers.in Board 12th Question Papers will be Provided Very Soon Through Online Mode and all the Applicants Should Visit the Official Page Regularly for More Information Regarding Previous Paper, Examination Date. Check the Below Provided Details for More Information About the Board 12th Model Paper.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter