-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2719-CountOfIntegers.go
More file actions
93 lines (85 loc) · 2.72 KB
/
2719-CountOfIntegers.go
File metadata and controls
93 lines (85 loc) · 2.72 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
// 2719. Count of Integers
// You are given two numeric strings num1 and num2 and two integers max_sum and min_sum.
// We denote an integer x to be good if:
// num1 <= x <= num2
// min_sum <= digit_sum(x) <= max_sum.
// Return the number of good integers. Since the answer may be large, return it modulo 10^9 + 7.
// Note that digit_sum(x) denotes the sum of the digits of x.
// Example 1:
// Input: num1 = "1", num2 = "12", min_sum = 1, max_sum = 8
// Output: 11
// Explanation: There are 11 integers whose sum of digits lies between 1 and 8 are 1,2,3,4,5,6,7,8,10,11, and 12. Thus, we return 11.
// Example 2:
// Input: num1 = "1", num2 = "5", min_sum = 1, max_sum = 5
// Output: 5
// Explanation: The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5.
// Constraints:
// 1 <= num1 <= num2 <= 10^22
// 1 <= min_sum <= max_sum <= 400
import "fmt"
// 题目实际上求的是区间 [num1,..num2] 中,数位和在 [min_sum,..max_sum] 的数的个数
func count(num1 string, num2 string, min_sum int, max_sum int) int {
const mod = 1e9 + 7
f := [23][220]int{}
for i := range f {
for j := range f[i] {
f[i][j] = -1
}
}
num := num2
var dfs func(int, int, bool) int
dfs = func(pos, s int, limit bool) int {
if pos >= len(num) {
if s >= min_sum && s <= max_sum {
return 1
}
return 0
}
if !limit && f[pos][s] != -1 {
return f[pos][s]
}
var ans int
up := 9
if limit {
up = int(num[pos] - '0')
}
for i := 0; i <= up; i++ {
ans = (ans + dfs(pos+1, s+i, limit && i == up)) % mod
}
if !limit {
f[pos][s] = ans
}
return ans
}
a := dfs(0, 0, true)
t := []byte(num1)
for i := len(t) - 1; i >= 0; i-- {
if t[i] != '0' {
t[i]--
break
}
t[i] = '9'
}
num = string(t)
f = [23][220]int{}
for i := range f {
for j := range f[i] {
f[i][j] = -1
}
}
b := dfs(0, 0, true)
return (a - b + mod) % mod
}
func main() {
// Example 1:
// Input: num1 = "1", num2 = "12", min_sum = 1, max_sum = 8
// Output: 11
// Explanation: There are 11 integers whose sum of digits lies between 1 and 8 are 1,2,3,4,5,6,7,8,10,11, and 12. Thus, we return 11.
fmt.Println(count("1","12",1,8)) // 11
// Example 2:
// Input: num1 = "1", num2 = "5", min_sum = 1, max_sum = 5
// Output: 5
// Explanation: The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5.
fmt.Println(count("1","5",1,5)) // 5
}