B-number

本文详细介绍了如何使用数位动态规划解决B-number问题,并提供了一份清晰易懂的C++实现代码。通过记忆化搜索的方法优化了算法效率,适用于处理大规模数值的计数问题。

B-number

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652

数位dp

这题是暑期集训的时候做的,昨天补了数位dp的记忆化搜索做法,把艾神的递推算法更新一下。

代码如下:

 1 #include<cstdio>
 2 #include<string>
 3 #include<cstring>
 4 #include<iostream>
 5 #define LL long long
 6 #define LEN 20
 7 #define is_1 2
 8 #define have_13 2
 9 #define mod_by_13 13
10 #define pre_same 2
11 using namespace std;
12 LL t[LEN];
13 LL dp[LEN][is_1][have_13][mod_by_13][pre_same];
14 string s;
15 LL len;
16 void init_t();
17 void solve();
18 int main(void){
19     init_t();
20     while(cin>>s){
21         len=s.size();
22         memset(dp,0,sizeof(dp));
23         dp[0][0][0][0][1]=1;
24         solve();
25         LL temp=0;
26         for(int i=0;i<is_1;++i)
27         for(int j=0;j<pre_same;++j)
28             temp+=dp[len][i][1][0][j];
29         cout<<temp<<endl;
30     }
31 }
32 void init_t(){
33     t[0]=1;
34     for(int i=1;i<LEN;++i)
35         t[i]=(t[i-1]*10)%13;
36 }
37 void solve(){
38     for(int a=0;a<len;++a)
39     for(int b=0;b<is_1;++b)
40     for(int c=0;c<have_13;++c)
41     for(int d=0;d<mod_by_13;++d)
42     for(int e=0;e<pre_same;++e)
43     if(dp[a][b][c][d][e]){
44         int r=(e?s[a]-'0':9);
45         for(int x=0;x<=r;++x){
46             dp[a+1][x==1][c|(b&&x==3)][(d+t[len-a-1]*x)%13][e&&x==r]
47                 +=dp[a][b][c][d][e];
48         }
49     }
50 }

 感觉还是太懒了,这两周金工实习这么好的机会,居然每天也只能写一道题,而且是水题。

转载于:https://www.cnblogs.com/barrier/p/6002539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值