-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3756.cpp
More file actions
57 lines (56 loc) · 1.94 KB
/
Copy path3756.cpp
File metadata and controls
57 lines (56 loc) · 1.94 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
class Solution {
public:
long long mod = 1e9 + 7;
long long inv(long long a) {
if (a == 1) return 1;
return (mod - mod / a) * inv(mod % a) % mod;
}
vector<int> sumAndMultiply(string s, vector<vector<int>>& queries) {
int n = s.size();
string ss;
vector<int> availables;
for (int i = 0; i < n; ++i) {
if (s[i] == '0') continue;
availables.push_back(i);
ss.push_back(s[i]);
}
int m = ss.size();
long long sum = 0;
long long factor = 1;
long long curr = 0;
vector<long long> sumSuffix(m + 1, 0);
vector<long long> stringSuffix(m + 1, 0);
vector<long long> factorPrefix(m + 2, 1);
for (int i = m - 1; i >= 0; --i) {
int digit = ss[i] - '0';
sum += digit;
curr += digit * factor;
factor *= 10;
curr %= mod;
factor %= mod;
sum %= mod;
sumSuffix[i] = sum;
stringSuffix[i] = curr;
factorPrefix[m - i] = factor;
}
vector<int> res;
for (auto& q : queries) {
int start = q[0];
int end = q[1];
int startIdx = lower_bound(availables.begin(), availables.end(), start) - availables.begin();
int endIdx = upper_bound(availables.begin(), availables.end(), end) - availables.begin() - 1;
if (startIdx == availables.size() || startIdx > endIdx) {
res.push_back(0);
continue;
}
// [startIdx, endIdx]
long long sum = sumSuffix[startIdx] - sumSuffix[endIdx + 1];
long long str = (stringSuffix[startIdx] - stringSuffix[endIdx + 1]) % mod;
int f = availables.size() - endIdx - 1;
str *= inv(factorPrefix[f]);
str %= mod;
res.push_back((((sum * str) % mod) + mod) % mod);
}
return res;
}
};