UVA11361 Investigating Div-Sum Property

Investigating Div-Sum Property

 题目大意:给定一个k,求a <= x <= b的,且x%k为0,x十进制下%k为0,有多少个x

 

题解:

数位dp套路题

ddp[i][j][a]表示i位数,%k为j,各位数加和%k为a的数有多少

初始化出i=1的状态,枚举往后面一位加的数b  dp[i+1][(j * 10 + b)%k][(j+b)%k] += dp[i][j][a]

 

直接求a与b之间的不好求,转为求1....b减去1....a-1

从高位到低位枚举,比如13245:
枚举0****,10***,11***,12***,130**,131**,1320*,1321*,1322*,1323*,分别用对应状态加入答案

然后枚举13240,13241,13242,13243,13244,13245,加入答案

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #define min(a, b) ((a) < (b) ? (a) : (b))
 9 #define max(a, b) ((a) > (b) ? (a) : (b))
10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
11 inline void swap(long long &a, long long &b)
12 {
13     long long tmp = a;a = b;b = tmp;
14 }
15 inline void read(long long &x)
16 {
17     x = 0;char ch = getchar(), c = ch;
18     while(ch < '0' || ch > '9') c = ch, ch = getchar();
19     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
20     if(c == '-') x = -x;
21 }
22 
23 const long long INF = 0x3f3f3f3f;
24 const long long MAXK = 100 + 10;
25 
26 long long t,a,b,k,dp[40][MAXK][MAXK],powp[40],n,tmp;
27 //dp[i][j][a]表示i位数,这个数%k=j,各位数之和%k=a
28 
29 
30 //求1...m有多少满足条件的数 
31 long long solve(long long m)
32 {
33     long long now = 0, sum = 0, ans = 0, ma = 0;
34     while(powp[ma] <= m) ++ ma;
35     for(register long long i = ma;i >= 2;-- i)
36     {
37         while(now + powp[i - 1] <= m) 
38         {
39             ans += dp[i - 1][(k - (now % k))%k][(k - (sum % k))%k];
40             now += powp[i - 1];
41             ++ sum;
42         }
43     }
44     for(;now <= m;++ now, ++ sum)
45         if(now % k == 0 && sum % k == 0) ++ ans;
46     return ans;
47 }
48 
49 int main()
50 {
51     powp[0] = 1;
52     for(register long long i = 1;i <= 35;++ i)powp[i] = powp[i - 1] * 10;
53     read(t);
54     for(;t;--t)
55     {
56         read(a), read(b), read(k);
57         if(k > 100)
58         {
59             printf("0\n");
60             continue;
61         }
62         memset(dp, 0, sizeof(dp));
63         n = 0;
64         while(powp[n] <= b) ++ n;
65         for(register long long i = 0;i <= 9;++ i) ++ dp[1][i % k][i % k];
66         for(register long long i = 1;i < n;++ i)
67             for(register long long j = 0;j < k;++ j)
68                 for(register long long a = 0;a < k;++ a)
69                     for(register long long b = 0;b <= 9;++ b)
70                         dp[i + 1][(j * 10 + b) % k][(a + b) % k] += dp[i][j][a];
71         printf("%lld\n", solve(b) - solve(a - 1));
72     }
73     return 0;
74 } 
UVA11361

 

posted @ 2018-01-18 20:09  嘒彼小星  阅读(195)  评论(0编辑  收藏  举报