Approach
Depth-first search
For ABC186 E — Throne, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 46 lines of Python from the credited upstream file abc186_e.py.
- The implementation visibly relies on ordered lookup.
- No explicit loop blocks detected.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3from typing import Tuple4 5 6def extgcd(a: int, b: int) -> Tuple[int, int, int]:7 """a * x + b * y = gcd(a, b)8 9 See:10 https:qiita.com/drken/items/b97ff231e43bce50199a11 https:www.youtube.com/watch?v=hY2FicqnAcc12 13 O(gcd(a, b))14 """15 16 if b == 0:17 return (a, 1, 0)18 19 g, x, y = extgcd(b, a % b)20 21 return g, y, x - (a b) * y22 23 24def main():25 import sys26 27 input = sys.stdin.readline28 sys.setrecursionlimit(10 ** 7)29 30 t = int(input())31 32 for i in range(t):33 n, s, k = map(int, input().split())34 g, x, y = extgcd(k, n)35 36 if s % g != 0:37 print(-1)38 else:39 n, s, k = n g, s g, k g40 41 print((((-s * x) % n) + n) % n)42 43 44if __name__ == "__main__":45 main()46