Skip to content

3535. Unit Conversion II 👎

  • Time: $O(n + q)$
  • Space: $O(n + q)$
 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
class Solution {
 public:
  vector<int> queryConversions(vector<vector<int>>& conversions,
                               vector<vector<int>>& queries) {
    const vector<int> units = baseUnitConversions(conversions);
    vector<int> ans;

    for (const vector<int>& query : queries) {
      const int u = query[0];
      const int v = query[1];
      // By Fermat's little theorem.
      ans.push_back(units[v] * modPow(units[u], kMod - 2) % kMod);
    }

    return ans;
  }

 private:
  static constexpr int kMod = 1'000'000'007;

  // Same as 3528. Unit Conversion I
  vector<int> baseUnitConversions(vector<vector<int>>& conversions) {
    const int n = conversions.size() + 1;
    vector<int> res(n);
    res[0] = 1;
    queue<int> q{{0}};
    vector<vector<pair<int, int>>> graph(n);

    for (const vector<int>& conversion : conversions) {
      const int u = conversion[0];
      const int v = conversion[1];
      const int factor = conversion[2];
      graph[u].emplace_back(v, factor);
    }

    while (!q.empty()) {
      const int u = q.front();
      q.pop();
      for (const auto& [v, factor] : graph[u]) {
        res[v] = (static_cast<long>(res[u]) * factor) % kMod;
        q.push(v);
      }
    }

    return res;
  }

  long modPow(long x, long n) {
    if (n == 0)
      return 1;
    if (n % 2 == 1)
      return x * modPow(x % kMod, (n - 1)) % kMod;
    return modPow(x * x % kMod, (n / 2)) % kMod;
  }
};
 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
class Solution {
  public int[] queryConversions(int[][] conversions, int[][] queries) {
    int[] units = baseUnitConversions(conversions);
    int[] ans = new int[queries.length];

    for (int i = 0; i < queries.length; ++i) {
      final int u = queries[i][0];
      final int v = queries[i][1];
      // By Fermat's little theorem.
      ans[i] = (int) ((long) units[v] * modPow(units[u], MOD - 2) % MOD);
    }

    return ans;
  }

  private static final int MOD = 1_000_000_007;

  private int[] baseUnitConversions(int[][] conversions) {
    final int n = conversions.length + 1;
    int[] ans = new int[n];
    ans[0] = 1;
    Queue<Integer> q = new ArrayDeque<>(Arrays.asList(0));
    List<Pair<Integer, Integer>>[] graph = new List[n];

    for (int i = 0; i < n; i++)
      graph[i] = new ArrayList<>();

    for (int[] conversion : conversions) {
      final int u = conversion[0];
      final int v = conversion[1];
      final int factor = conversion[2];
      graph[u].add(new Pair<>(v, factor));
    }

    while (!q.isEmpty()) {
      final int u = q.poll();
      for (Pair<Integer, Integer> pair : graph[u]) {
        final int v = pair.getKey();
        final int factor = pair.getValue();
        ans[v] = (int) ((long) ans[u] * factor % MOD);
        q.offer(v);
      }
    }

    return ans;
  }

  private int modPow(long x, long n) {
    if (n == 0)
      return 1;
    if (n % 2 == 1)
      return (int) (x * modPow(x % MOD, (n - 1)) % MOD);
    return modPow(x * x % MOD, (n / 2)) % MOD;
  }
}
 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
class Solution:
  def queryConversions(
      self,
      conversions: list[list[int]],
      queries: list[list[int]]
  ) -> list[int]:
    self.MOD = 1_000_000_007
    units = self._baseUnitConversions(conversions)
    # By Fermat's little theorem.
    return [units[v] * self._modPow(units[u], self.MOD - 2) % self.MOD
            for u, v in queries]

  # Same as 3528. Unit Conversion I
  def _baseUnitConversions(self, conversions: list[list[int]]) -> list[int]:
    n = len(conversions) + 1
    res = [0] * n
    res[0] = 1
    q = collections.deque([0])
    graph = [[] for _ in range(n)]

    for u, v, factor in conversions:
      graph[u].append((v, factor))

    while q:
      u = q.popleft()
      for v, factor in graph[u]:
        res[v] = (res[u] * factor) % self.MOD
        q.append(v)

    return res

  def _modPow(self, x: int, n: int) -> int:
    if n == 0:
      return 1
    if n % 2 == 1:
      return x * self._modPow(x, n - 1) % self.MOD
    return self._modPow(x * x % self.MOD, n // 2)