Ivan the Terrible

Given three integer numbers n, a and b, does there exist a natural t
such that a^(t) ≡ b mod  n?

Input

Input consists of the number of cases c, followed by c triples with n, a
and b. You can assume 2 ≤ n ≤ 10⁹, 0 ≤ a < n, and 0 ≤ b < n.
Additionally, assume c ≤ 200 for the “hard private test cases”.

Output

For each case, print “YES” or “NO” depending on whether a^(t) ≡ b mod  n
has at least one solution t ≥ 0 or not.

Problem information

Author: Ivan Geffner

Generation: 2026-01-25T10:31:11.350Z

© Jutge.org, 2006–2026.
https://jutge.org
