light oj 1068 数位dp

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <math.h>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <climits>
 8 #include <queue>
 9 #define ll long long
10 
11 using namespace std;
12 const int N = 1e4+1000;
13 
14 int digit[25],l,k;
15 ll dp[25][100][100];
16 
17 void init()
18 {
19     memset(dp,-1,sizeof(dp));
20 }
21 
22 ll dfs( int cur, int n, int sum, int f)
23 {
24     if(cur == -1)
25     {
26         return (n == 0 && sum == 0) ?1:0;
27     }
28 
29     if(!f && dp[cur][n][sum] != -1)
30         return dp[cur][n][sum];
31         
32     ll ans = 0;
33     int len = f?digit[cur]:9;
34     for(int i = 0; i <= len; i++)
35     {
36         ans += dfs(cur-1, (n*10 + i)%k, (sum+i)%k, f&(i == len));
37     }
38     if(!f)
39         return dp[cur][n][sum] = ans;
40     else
41         return ans;
42 }
43 
44 ll work(int num,int mod)
45 {
46     l = 0;
47     while(num > 0)
48     {
49         digit[l++] = num%10;
50         num /= 10;
51     }
52     ll ans = dfs(l-1, 0, 0, 1);
53     return ans;
54 }
55 
56 void solve()
57 {
58     int a,b;
59 
60     init();
61     scanf("%d %d %d",&a,&b,&k);
62     if(k > 90)
63     {
64         printf("0\n");
65         return ;
66     }
67     printf("%lld\n", work(b,k) - work(a-1,k));
68 
69 }
70 
71 int main(void)
72 {
73 
74     int t,cnt = 0;
75     scanf("%d",&t);
76     while(t--)
77     {
78         printf("Case %d: ",++cnt);
79         solve();
80     }
81     return 0;
82 }

 

转载于:https://www.cnblogs.com/henserlinda/p/5745837.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值