BZOJ1833(数位dp)

这个数位dp倒是没什么限制条件,只是需要在过程中把每个数字出现次数记录一下即可。记忆化返回时数学算出。框架还是套板子。

 

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 typedef long long ll;
 9 ll n, m, cnt1[10], cnt2[10], dp[20][2];
10 int tot, a[20];
11 
12 ll dfs(int pos, int state0, int limit, ll *cnt) {
13     if (!pos) {
14         cnt[0] += state0;
15         return 1;
16     }
17     if (!limit && !state0 && dp[pos][state0] != -1) {
18         for (int i = 0; i <= 9; i++) {
19             cnt[i] += pow(10, pos - 1) * pos;
20         }
21         return dp[pos][state0];
22     }
23 
24     int up = limit ? a[pos] : 9;
25     ll ret = 0;
26 
27     for (int i = 0; i <= up; ++i) {
28         ll tmp = dfs(pos - 1, state0 & (!i), limit & (i == a[pos]), cnt);
29         cnt[i] += tmp;
30         if (i == 0 && state0)    cnt[i] -= tmp;
31         ret += tmp;
32     }
33 
34     if (!limit)    dp[pos][state0] = ret;
35     return ret;
36 }
37 
38 void solve(ll x, ll *cnt) {
39     for (tot = 0; x; x /= 10)
40         a[++tot] = x % 10;
41     dfs(tot, 1, 1, cnt);
42 }
43 
44 int main() {
45     memset(dp, -1, sizeof dp);
46 
47     cin >> n >> m;
48     solve(m, cnt1);
49     solve(n - 1, cnt2);
50 
51     for (int i = 0; i <= 9; ++i)
52         printf("%lld ", cnt1[i] - cnt2[i]);
53     
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/AlphaWA/p/10650132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值