-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-of-integers.cpp
82 lines (76 loc) · 2.9 KB
/
count-of-integers.cpp
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
// Time: O(n * m), m = max_sum
// Space: O(m)
// combinatorics, dp
class Solution {
public:
int count(string num1, string num2, int min_sum, int max_sum) {
static const int MOD = 1e9 + 7;
const auto& check = [&](const auto& x) {
const auto& total = accumulate(cbegin(x), cend(x), 0, [](const auto& accu, const auto& c) {
return accu + (c - '0');
});
return min_sum <= total && total <= max_sum;
};
const auto& f = [&](const auto& x) {
vector<vector<int>> dp(2, vector<int>(max_sum + 1));
dp[0][0] = dp[1][0] = 1;
for (int i = size(x) - 1; i >= 0; --i) {
vector<vector<int>> new_dp(2, vector<int>(max_sum + 1));
for (int t = 0; t < 2; ++t) {
for (int total = 0; total <= max_sum; ++total) {
for (int d = 0; d <= min((t == 1 ? x[i] - '0' : 9), total); ++d) {
new_dp[t][total] = (new_dp[t][total] + dp[static_cast<int>(t && d == x[i] - '0')][total - d]) % MOD;
}
}
}
dp = move(new_dp);
}
int result = 0;
for (int total = min_sum; total <= max_sum; ++total) {
result = (result + dp[1][total]) % MOD;
}
return result;
};
return (((f(num2) - f(num1)) % MOD + MOD) % MOD + check(num1)) % MOD;
}
};
// Time: O(n * m), m = max_sum
// Space: O(m)
// combinatorics, dp
class Solution2 {
public:
int count(string num1, string num2, int min_sum, int max_sum) {
static const int MOD = 1e9 + 7;
const auto& dec = [](auto& x) {
for (int i = size(x) - 1; i >= 0; --i) {
if (x[i] != '0') {
--x[i];
break;
}
x[i] = '9';
}
return x;
};
const auto& f = [&](const auto& x) {
vector<vector<int>> dp(2, vector<int>(max_sum + 1));
dp[0][0] = dp[1][0] = 1;
for (int i = size(x) - 1; i >= 0; --i) {
vector<vector<int>> new_dp(2, vector<int>(max_sum + 1));
for (int t = 0; t < 2; ++t) {
for (int total = 0; total <= max_sum; ++total) {
for (int d = 0; d <= min((t == 1 ? x[i] - '0' : 9), total); ++d) {
new_dp[t][total] = (new_dp[t][total] + dp[static_cast<int>(t && d == x[i] - '0')][total - d]) % MOD;
}
}
}
dp = move(new_dp);
}
int result = 0;
for (int total = min_sum; total <= max_sum; ++total) {
result = (result + dp[1][total]) % MOD;
}
return result;
};
return ((f(num2) - f(dec(num1)) % MOD) + MOD) % MOD;
}
};