-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathunit-conversion-ii.cpp
More file actions
55 lines (50 loc) · 1.64 KB
/
unit-conversion-ii.cpp
File metadata and controls
55 lines (50 loc) · 1.64 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
// Time: O(n + qlogm)
// Space: O(n)
// bfs, fast exponentiation
class Solution {
public:
vector<int> queryConversions(vector<vector<int>>& conversions, vector<vector<int>>& queries) {
static const int MOD = 1e9 + 7;
const auto& powmod = [&](int a, int b) {
a %= MOD;
int64_t result = 1;
while (b) {
if (b & 1) {
result = result * a % MOD;
}
a = int64_t(a) * a % MOD;
b >>= 1;
}
return result;
};
const auto& divmod = [&](int a, int b) {
return (a * powmod(b, MOD - 2)) % MOD;
};
const auto& unit = [&]() {
vector<vector<pair<int, int>>> adj(size(conversions) + 1);
for (const auto& c : conversions) {
adj[c[0]].emplace_back(c[1], c[2]);
}
vector<int> result(size(adj));
result[0] = 1;
vector<int> q = {0};
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
for (const auto& [v, w] : adj[u]) {
result[v] = (static_cast<int64_t>(result[u]) * w) % MOD;
new_q.emplace_back(v);
}
}
q = move(new_q);
}
return result;
};
const auto& lookup = unit();
vector<int> result(size(queries));
for (int i = 0; i < size(queries); ++i) {
result[i] = divmod(lookup[queries[i][1]], lookup[queries[i][0]]);
}
return result;
}
};