結果

問題 No.981 一般冪乗根
ユーザー PachicobuePachicobue
提出日時 2022-08-10 13:58:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 6,000 ms
コード長 31,143 bytes
コンパイル時間 3,020 ms
コンパイル使用メモリ 242,092 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-21 00:29:15
合計ジャッジ時間 56,424 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,248 KB
testcase_01 AC 6 ms
5,376 KB
testcase_02 AC 5 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 5 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 15 ms
5,376 KB
evil_60bit1.txt AC 13 ms
5,376 KB
evil_60bit2.txt AC 13 ms
5,376 KB
evil_60bit3.txt AC 12 ms
5,376 KB
evil_hack AC 2 ms
5,376 KB
evil_hard_random AC 12 ms
5,376 KB
evil_hard_safeprime.txt AC 16 ms
5,376 KB
evil_hard_tonelli0 AC 12 ms
5,376 KB
evil_hard_tonelli1 AC 2,809 ms
5,376 KB
evil_hard_tonelli2 AC 153 ms
5,376 KB
evil_hard_tonelli3 AC 357 ms
5,376 KB
evil_sefeprime1.txt AC 14 ms
5,376 KB
evil_sefeprime2.txt AC 13 ms
5,376 KB
evil_sefeprime3.txt AC 15 ms
5,376 KB
evil_tonelli1.txt AC 4,267 ms
5,376 KB
evil_tonelli2.txt AC 4,294 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using f64 = double;
using f80 = long double;
using f128 = __float128;
constexpr i32 operator"" _i32(u64 v)
{
    return v;
}
constexpr i32 operator"" _u32(u64 v)
{
    return v;
}
constexpr i64 operator"" _i64(u64 v)
{
    return v;
}
constexpr u64 operator"" _u64(u64 v)
{
    return v;
}
constexpr f64 operator"" _f64(f80 v)
{
    return v;
}
constexpr f80 operator"" _f80(f80 v)
{
    return v;
}
using Istream = std::istream;
using Ostream = std::ostream;
using Str = std::string;
template<typename T>
using Lt = std::less<T>;
template<typename T>
using Gt = std::greater<T>;
template<typename T>
using IList = std::initializer_list<T>;
template<int n>
using BSet = std::bitset<n>;
template<typename T1, typename T2>
using Pair = std::pair<T1, T2>;
template<typename... Ts>
using Tup = std::tuple<Ts...>;
template<typename T, int N>
using Arr = std::array<T, N>;
template<typename... Ts>
using Deq = std::deque<Ts...>;
template<typename... Ts>
using Set = std::set<Ts...>;
template<typename... Ts>
using MSet = std::multiset<Ts...>;
template<typename... Ts>
using USet = std::unordered_set<Ts...>;
template<typename... Ts>
using UMSet = std::unordered_multiset<Ts...>;
template<typename... Ts>
using Map = std::map<Ts...>;
template<typename... Ts>
using MMap = std::multimap<Ts...>;
template<typename... Ts>
using UMap = std::unordered_map<Ts...>;
template<typename... Ts>
using UMMap = std::unordered_multimap<Ts...>;
template<typename... Ts>
using Vec = std::vector<Ts...>;
template<typename... Ts>
using Stack = std::stack<Ts...>;
template<typename... Ts>
using Queue = std::queue<Ts...>;
template<typename T>
using MaxHeap = std::priority_queue<T>;
template<typename T>
using MinHeap = std::priority_queue<T, Vec<T>, Gt<T>>;
using NSec = std::chrono::nanoseconds;
using USec = std::chrono::microseconds;
using MSec = std::chrono::milliseconds;
using Sec = std::chrono::seconds;
template<typename T>
constexpr T LIMMIN = std::numeric_limits<T>::min();
template<typename T>
constexpr T LIMMAX = std::numeric_limits<T>::max();
template<typename T>
constexpr T INF = (LIMMAX<T> - 1) / 2;
template<typename T>
constexpr T PI = T{3.141592653589793238462643383279502884};
template<typename T = u64>
constexpr T TEN(const int n)
{
    return n == 0 ? T{1} : TEN<T>(n - 1) * T{10};
}
Ostream& operator<<(Ostream& os, i128 v)
{
    bool minus = false;
    if (v < 0) { minus = true, v = -v; }
    Str ans;
    if (v == 0) { ans = "0"; }
    while (v) {
        ans.push_back('0' + v % 10), v /= 10;
    }
    std::reverse(ans.begin(), ans.end());
    return os << (minus ? "-" : "") << ans;
}
Ostream& operator<<(Ostream& os, u128 v)
{
    Str ans;
    if (v == 0) { ans = "0"; }
    while (v) {
        ans.push_back('0' + v % 10), v /= 10;
    }
    std::reverse(ans.begin(), ans.end());
    return os << ans;
}
template<typename T>
bool chmin(T& a, const T& b)
{
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template<typename T>
bool chmax(T& a, const T& b)
{
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template<typename T>
constexpr T floorDiv(T x, T y)
{
    if (y < T{}) { x = -x, y = -y; }
    return x >= T{} ? x / y : (x - y + 1) / y;
}
template<typename T>
constexpr T ceilDiv(T x, T y)
{
    if (y < T{}) { x = -x, y = -y; }
    return x >= T{} ? (x + y - 1) / y : x / y;
}
template<typename T, typename I>
constexpr T modPower(T v, I n, T mod)
{
    T ans = 1 % mod;
    for (; n > 0; n >>= 1, (v *= v) %= mod) {
        if (n % 2 == 1) { (ans *= v) %= mod; }
    }
    return ans;
}
template<typename T, typename I>
constexpr T power(T v, I n)
{
    T ans = 1;
    for (; n > 0; n >>= 1, v *= v) {
        if (n % 2 == 1) { ans *= v; }
    }
    return ans;
}
template<typename T, typename I>
constexpr T power(T v, I n, const T& e)
{
    T ans = e;
    for (; n > 0; n >>= 1, v *= v) {
        if (n % 2 == 1) { ans *= v; }
    }
    return ans;
}
template<typename T>
Vec<T>& operator+=(Vec<T>& vs1, const Vec<T>& vs2)
{
    vs1.insert(vs1.end(), vs2.begin(), vs2.end());
    return vs1;
}
template<typename T>
Vec<T> operator+(const Vec<T>& vs1, const Vec<T>& vs2)
{
    auto vs = vs1;
    vs += vs2;
    return vs;
}
template<typename Vs, typename V>
void fillAll(Vs& arr, const V& v)
{
    if constexpr (std::is_convertible<V, Vs>::value) {
        arr = v;
    } else {
        for (auto& subarr : arr) {
            fillAll(subarr, v);
        }
    }
}
template<typename Vs>
void sortAll(Vs& vs)
{
    std::sort(std::begin(vs), std::end(vs));
}
template<typename Vs, typename C>
void sortAll(Vs& vs, C comp)
{
    std::sort(std::begin(vs), std::end(vs), comp);
}
template<typename Vs>
void reverseAll(Vs& vs)
{
    std::reverse(std::begin(vs), std::end(vs));
}
template<typename V, typename Vs>
V sumAll(const Vs& vs)
{
    if constexpr (std::is_convertible<Vs, V>::value) {
        return static_cast<V>(vs);
    } else {
        V ans = 0;
        for (const auto& v : vs) {
            ans += sumAll<V>(v);
        }
        return ans;
    }
}
template<typename Vs>
int minInd(const Vs& vs)
{
    return std::min_element(std::begin(vs), std::end(vs)) - std::begin(vs);
}
template<typename Vs>
int maxInd(const Vs& vs)
{
    return std::max_element(std::begin(vs), std::end(vs)) - std::begin(vs);
}
template<typename Vs, typename V>
int lbInd(const Vs& vs, const V& v)
{
    return std::lower_bound(std::begin(vs), std::end(vs), v) - std::begin(vs);
}
template<typename Vs, typename V>
int ubInd(const Vs& vs, const V& v)
{
    return std::upper_bound(std::begin(vs), std::end(vs), v) - std::begin(vs);
}
template<typename Vs, typename V>
bool contains(const Vs& vs, const V& v)
{
    const int li = lbInd(vs, v);
    return (li < std::size(vs) and vs[li] == v);
}
template<typename Vs, typename V>
void plusAll(Vs& vs, const V& v)
{
    for (auto& v_ : vs) {
        v_ += v;
    }
}
template<typename T, typename F>
Vec<T> genVec(int n, F gen)
{
    Vec<T> ans;
    std::generate_n(std::back_insert_iterator(ans), n, gen);
    return ans;
}
template<typename T = int>
Vec<T> iotaVec(int n, T offset = 0)
{
    Vec<T> ans(n);
    std::iota(ans.begin(), ans.end(), offset);
    return ans;
}
constexpr int popcount(const u64 v)
{
    return v ? __builtin_popcountll(v) : 0;
}
constexpr int log2p1(const u64 v)
{
    return v ? 64 - __builtin_clzll(v) : 0;
}
constexpr int lsbp1(const u64 v)
{
    return __builtin_ffsll(v);
}
constexpr int ceillog(const u64 v)
{
    return v ? log2p1(v - 1) : 0;
}
constexpr u64 ceil2(const u64 v)
{
    const int l = ceillog(v);
    return (l == 64) ? 0_u64 : (1_u64 << l);
}
constexpr u64 floor2(const u64 v)
{
    return v ? (1_u64 << (log2p1(v) - 1)) : 0_u64;
}
constexpr bool ispow2(const u64 v)
{
    return (v > 0) and ((v & (v - 1)) == 0);
}
constexpr bool btest(const u64 mask, const int ind)
{
    return (mask >> ind) & 1_u64;
}
template<typename F>
struct Fix : F
{
    Fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args>
    auto operator()(Args&&... args) const
    {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};
class irange
{
private:
    struct itr
    {
        itr(i64 start = 0, i64 step = 1) : m_cnt{start}, m_step{step} {}
        bool operator!=(const itr& it) const
        {
            return m_cnt != it.m_cnt;
        }
        i64 operator*()
        {
            return m_cnt;
        }
        itr& operator++()
        {
            m_cnt += m_step;
            return *this;
        }
        i64 m_cnt, m_step;
    };
    i64 m_start, m_end, m_step;
public:
    irange(i64 start, i64 end, i64 step = 1)
    {
        assert(step != 0);
        const i64 d = std::abs(step);
        const i64 l = (step > 0 ? start : end);
        const i64 r = (step > 0 ? end : start);
        i64 n = (r - l) / d + ((r - l) % d ? 1 : 0);
        if (l >= r) { n = 0; }
        m_start = start;
        m_end = start + step * n;
        m_step = step;
    }
    itr begin() const
    {
        return itr{m_start, m_step};
    }
    itr end() const
    {
        return itr{m_end, m_step};
    }
};
irange rep(i64 end)
{
    return irange(0, end, 1);
}
irange per(i64 rend)
{
    return irange(rend - 1, -1, -1);
}
/**
 * @ref https://prng.di.unimi.it
 */
namespace xoshiro_impl {
u64 x;
u64 next()
{
    uint64_t z = (x += 0x9e3779b97f4a7c15);
    z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
    z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
    return z ^ (z >> 31);
}
} // namespace xoshiro_impl
class Xoshiro32
{
public:
    using result_type = u32;
    using T = result_type;
    Xoshiro32(T seed = 0)
    {
        xoshiro_impl::x = seed;
        s[0] = xoshiro_impl::next();
        s[1] = xoshiro_impl::next();
        s[2] = xoshiro_impl::next();
        s[3] = xoshiro_impl::next();
    }
    static constexpr T min()
    {
        return LIMMIN<T>;
    }
    static constexpr T max()
    {
        return LIMMAX<T>;
    }
    T operator()()
    {
        return next();
    }
private:
    static constexpr T rotl(const T x, int k)
    {
        return (x << k) | (x >> (32 - k));
    }
    T next()
    {
        const T ans = rotl(s[1] * 5, 7) * 9;
        const T t = s[1] << 9;
        s[2] ^= s[0];
        s[3] ^= s[1];
        s[1] ^= s[2];
        s[0] ^= s[3];
        s[2] ^= t;
        s[3] = rotl(s[3], 11);
        return ans;
    }
    T s[4];
};
class Xoshiro64
{
public:
    using result_type = u64;
    using T = result_type;
    Xoshiro64(T seed = 0)
    {
        xoshiro_impl::x = seed;
        s[0] = xoshiro_impl::next();
        s[1] = xoshiro_impl::next();
        s[2] = xoshiro_impl::next();
        s[3] = xoshiro_impl::next();
    }
    static constexpr T min()
    {
        return LIMMIN<T>;
    }
    static constexpr T max()
    {
        return LIMMAX<T>;
    }
    T operator()()
    {
        return next();
    }
private:
    static constexpr T rotl(const T x, int k)
    {
        return (x << k) | (x >> (64 - k));
    }
    T next()
    {
        const T ans = rotl(s[1] * 5, 7) * 9;
        const T t = s[1] << 17;
        s[2] ^= s[0];
        s[3] ^= s[1];
        s[1] ^= s[2];
        s[0] ^= s[3];
        s[2] ^= t;
        s[3] = rotl(s[3], 45);
        return ans;
    }
    T s[4];
};
template<typename Rng>
class RNG
{
public:
    using result_type = typename Rng::result_type;
    using T = result_type;
    static constexpr T min()
    {
        return Rng::min();
    }
    static constexpr T max()
    {
        return Rng::max();
    }
    RNG() : RNG(std::random_device{}()) {}
    RNG(T seed) : m_rng(seed) {}
    T operator()()
    {
        return m_rng();
    }
    template<typename T>
    T val(T min, T max)
    {
        return std::uniform_int_distribution<T>(min, max)(m_rng);
    }
    template<typename T>
    Pair<T, T> pair(T min, T max)
    {
        return std::minmax({val<T>(min, max), val<T>(min, max)});
    }
    template<typename T>
    Vec<T> vec(int n, T min, T max)
    {
        return genVec<T>(n, [&]() { return val<T>(min, max); });
    }
    template<typename T>
    Vec<Vec<T>> vvec(int n, int m, T min, T max)
    {
        return genVec<Vec<T>>(n, [&]() { return vec(m, min, max); });
    }
private:
    Rng m_rng;
};
RNG<std::mt19937> rng;
RNG<std::mt19937_64> rng64;
RNG<Xoshiro32> rng_xo;
RNG<Xoshiro64> rng_xo64;
template<typename K, typename V, int LG = 20>
class IntDict
{
public:
    IntDict() = default;
    V& operator[](K k)
    {
        const auto i = index(k);
        if (not m_used.test(i)) {
            m_used.set(i), m_keys[i] = k;
            m_vals[i] = V{};
        }
        return m_vals[i];
    }
    const V& at(K k) const
    {
        return m_vals[index(k)];
    }
    void erase(K k)
    {
        m_used.reset(index(k));
    }
    bool contains(K k) const
    {
        const auto i = index(k);
        return m_used.test(i) and m_keys[i] == k;
    }
private:
    u32 index(K k) const
    {
        u32 i = 0;
        for (i = fibHash(k); m_used.test(i) and m_keys[i] != k;
             (i += 1) &= (N - 1)) {}
        return i;
    }
    static constexpr int N = 1 << LG;
    static constexpr u32 fibHash(u64 k)
    {
        constexpr u64 a = 11400714819323198485_u64;
        return (a * k) >> (64 - LG);
    }
    BSet<N> m_used;
    Arr<K, N> m_keys;
    Arr<V, N> m_vals;
};
template<typename T>
constexpr Pair<T, T> extgcd(const T a, const T b) // [x,y] -> ax+by=gcd(a,b)
{
    static_assert(std::is_signed<T>::value,
                  "extgcd: Only signed integer supported.");
    assert(a != 0 or b != 0);
    if (a >= 0 and b >= 0) {
        if (a < b) {
            const auto [y, x] = extgcd(b, a);
            return {x, y};
        }
        if (b == 0) { return {1, 0}; }
        const auto [x, y] = extgcd(b, a % b);
        return {y, x - (a / b) * y};
    } else {
        auto [x, y] = extgcd(std::abs(a), std::abs(b));
        if (a < 0) { x = -x; }
        if (b < 0) { y = -y; }
        return {x, y};
    }
}
template<typename T>
constexpr T inverse(const T a, const T mod) // ax=gcd(a,M) (mod M)
{
    assert(a > 0 and mod > 0);
    auto [x, y] = extgcd(a, mod);
    if (x <= 0) { x += mod; }
    return x;
}
template<u32 mod_, u32 root_, u32 max2p_>
class modint
{
    template<typename U = u32&>
    static U modRef()
    {
        static u32 s_mod = 0;
        return s_mod;
    }
    template<typename U = u32&>
    static U rootRef()
    {
        static u32 s_root = 0;
        return s_root;
    }
    template<typename U = u32&>
    static U max2pRef()
    {
        static u32 s_max2p = 0;
        return s_max2p;
    }
public:
    static constexpr bool isDynamic()
    {
        return (mod_ == 0);
    }
    template<typename U = const u32>
    static constexpr std::enable_if_t<mod_ != 0, U> mod()
    {
        return mod_;
    }
    template<typename U = const u32>
    static std::enable_if_t<mod_ == 0, U> mod()
    {
        return modRef();
    }
    template<typename U = const u32>
    static constexpr std::enable_if_t<mod_ != 0, U> root()
    {
        return root_;
    }
    template<typename U = const u32>
    static std::enable_if_t<mod_ == 0, U> root()
    {
        return rootRef();
    }
    template<typename U = const u32>
    static constexpr std::enable_if_t<mod_ != 0, U> max2p()
    {
        return max2p_;
    }
    template<typename U = const u32>
    static std::enable_if_t<mod_ == 0, U> max2p()
    {
        return max2pRef();
    }
    template<typename U = u32>
    static void setMod(std::enable_if_t<mod_ == 0, U> m)
    {
        modRef() = m;
    }
    template<typename U = u32>
    static void setRoot(std::enable_if_t<mod_ == 0, U> r)
    {
        rootRef() = r;
    }
    template<typename U = u32>
    static void setMax2p(std::enable_if_t<mod_ == 0, U> m)
    {
        max2pRef() = m;
    }
    constexpr modint() : m_val{0} {}
    constexpr modint(i64 v) : m_val{normll(v)} {}
    constexpr void setRaw(u32 v)
    {
        m_val = v;
    }
    constexpr modint operator-() const
    {
        return modint{0} - (*this);
    }
    constexpr modint& operator+=(const modint& m)
    {
        m_val = norm(m_val + m.val());
        return *this;
    }
    constexpr modint& operator-=(const modint& m)
    {
        m_val = norm(m_val + mod() - m.val());
        return *this;
    }
    constexpr modint& operator*=(const modint& m)
    {
        m_val = normll((i64)m_val * (i64)m.val() % (i64)mod());
        return *this;
    }
    constexpr modint& operator/=(const modint& m)
    {
        return *this *= m.inv();
    }
    constexpr modint operator+(const modint& m) const
    {
        auto v = *this;
        return v += m;
    }
    constexpr modint operator-(const modint& m) const
    {
        auto v = *this;
        return v -= m;
    }
    constexpr modint operator*(const modint& m) const
    {
        auto v = *this;
        return v *= m;
    }
    constexpr modint operator/(const modint& m) const
    {
        auto v = *this;
        return v /= m;
    }
    constexpr bool operator==(const modint& m) const
    {
        return m_val == m.val();
    }
    constexpr bool operator!=(const modint& m) const
    {
        return not(*this == m);
    }
    friend Istream& operator>>(Istream& is, modint& m)
    {
        i64 v;
        return is >> v, m = v, is;
    }
    friend Ostream& operator<<(Ostream& os, const modint& m)
    {
        return os << m.val();
    }
    constexpr u32 val() const
    {
        return m_val;
    }
    template<typename I>
    constexpr modint pow(I n) const
    {
        return power(*this, n);
    }
    constexpr modint inv() const
    {
        return pow(mod() - 2);
    }
    static modint sinv(u32 n)
    {
        static Vec<modint> is{1, 1};
        for (u32 i = (u32)is.size(); i <= n; i++) {
            is.push_back(-is[mod() % i] * (mod() / i));
        }
        return is[n];
    }
    static modint fact(u32 n)
    {
        static Vec<modint> fs{1, 1};
        for (u32 i = (u32)fs.size(); i <= n; i++) {
            fs.push_back(fs.back() * i);
        }
        return fs[n];
    }
    static modint ifact(u32 n)
    {
        static Vec<modint> ifs{1, 1};
        for (u32 i = (u32)ifs.size(); i <= n; i++) {
            ifs.push_back(ifs.back() * sinv(i));
        }
        return ifs[n];
    }
    static modint comb(int n, int k)
    {
        return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k) * ifact(k);
    }
private:
    static constexpr u32 norm(u32 x)
    {
        return x < mod() ? x : x - mod();
    }
    static constexpr u32 normll(i64 x)
    {
        return norm(u32(x % (i64)mod() + (i64)mod()));
    }
    u32 m_val;
};
using modint_1000000007 = modint<1000000007, 5, 1>;
using modint_998244353 = modint<998244353, 3, 23>;
template<int id>
using modint_dynamic = modint<0, 0, id>;
template<u64 mod_, u64 root_, u64 max2p_>
class modint64
{
    template<typename U = u64&>
    static U modRef()
    {
        static u64 s_mod = 0;
        return s_mod;
    }
    template<typename U = u64&>
    static U rootRef()
    {
        static u64 s_root = 0;
        return s_root;
    }
    template<typename U = u64&>
    static U max2pRef()
    {
        static u64 s_max2p = 0;
        return s_max2p;
    }
public:
    static constexpr bool isDynamic()
    {
        return (mod_ == 0);
    }
    template<typename U = const u64>
    static constexpr std::enable_if_t<mod_ != 0, U> mod()
    {
        return mod_;
    }
    template<typename U = const u64>
    static std::enable_if_t<mod_ == 0, U> mod()
    {
        return modRef();
    }
    template<typename U = const u64>
    static constexpr std::enable_if_t<mod_ != 0, U> root()
    {
        return root_;
    }
    template<typename U = const u64>
    static std::enable_if_t<mod_ == 0, U> root()
    {
        return rootRef();
    }
    template<typename U = const u64>
    static constexpr std::enable_if_t<mod_ != 0, U> max2p()
    {
        return max2p_;
    }
    template<typename U = const u64>
    static std::enable_if_t<mod_ == 0, U> max2p()
    {
        return max2pRef();
    }
    template<typename U = u64>
    static void setMod(std::enable_if_t<mod_ == 0, U> m)
    {
        modRef() = m;
    }
    template<typename U = u64>
    static void setRoot(std::enable_if_t<mod_ == 0, U> r)
    {
        rootRef() = r;
    }
    template<typename U = u64>
    static void setMax2p(std::enable_if_t<mod_ == 0, U> m)
    {
        max2pRef() = m;
    }
    constexpr modint64() : m_val{0} {}
    constexpr modint64(const i64 v) : m_val{normLL(v)} {}
    constexpr void setRaw(const u64 v)
    {
        m_val = v;
    }
    constexpr modint64 operator+() const
    {
        return *this;
    }
    constexpr modint64 operator-() const
    {
        return modint64{0} - (*this);
    }
    constexpr modint64& operator+=(const modint64& m)
    {
        m_val = norm(m_val + m.val());
        return *this;
    }
    constexpr modint64& operator-=(const modint64& m)
    {
        m_val = norm(m_val + mod() - m.val());
        return *this;
    }
    constexpr modint64& operator*=(const modint64& m)
    {
        m_val = normLL((i128)m_val * (i128)m.val() % (i128)mod());
        return *this;
    }
    constexpr modint64& operator/=(const modint64& m)
    {
        return *this *= m.inv();
    }
    constexpr modint64 operator+(const modint64& m) const
    {
        auto v = *this;
        return v += m;
    }
    constexpr modint64 operator-(const modint64& m) const
    {
        auto v = *this;
        return v -= m;
    }
    constexpr modint64 operator*(const modint64& m) const
    {
        auto v = *this;
        return v *= m;
    }
    constexpr modint64 operator/(const modint64& m) const
    {
        auto v = *this;
        return v /= m;
    }
    constexpr bool operator==(const modint64& m) const
    {
        return m_val == m.val();
    }
    constexpr bool operator!=(const modint64& m) const
    {
        return not(*this == m);
    }
    friend Istream& operator>>(Istream& is, modint64& m)
    {
        i64 v;
        return is >> v, m = v, is;
    }
    friend Ostream& operator<<(Ostream& os, const modint64& m)
    {
        return os << m.val();
    }
    constexpr u64 val() const
    {
        return m_val;
    }
    template<typename I>
    constexpr modint64 pow(I n) const
    {
        return power(*this, n);
    }
    constexpr modint64 inv() const
    {
        return pow(mod() - 2);
    }
    modint64 sinv() const
    {
        return sinv(m_val);
    }
    static modint64 fact(u32 n)
    {
        static Vec<modint64> fs{1, 1};
        for (u32 i = (u32)fs.size(); i <= n; i++) {
            fs.push_back(fs.back() * i);
        }
        return fs[n];
    }
    static modint64 ifact(u32 n)
    {
        static Vec<modint64> ifs{1, 1};
        for (u32 i = (u32)ifs.size(); i <= n; i++) {
            ifs.push_back(ifs.back() * sinv(i));
        }
        return ifs[n];
    }
    static modint64 perm(int n, int k)
    {
        return k > n or k < 0 ? modint64{0} : fact(n) * ifact(n - k);
    }
    static modint64 comb(int n, int k)
    {
        return k > n or k < 0 ? modint64{0} : fact(n) * ifact(n - k) * ifact(k);
    }
private:
    static constexpr u64 norm(const u64 x)
    {
        return x < mod() ? x : x - mod();
    }
    static constexpr u64 normLL(const i64 x)
    {
        return norm(u64((i128)x % (i128)mod() + (i128)mod()));
    }
    static modint64 sinv(u32 n)
    {
        static Vec<modint64> is{1, 1};
        for (u32 i = (u32)is.size(); i <= n; i++) {
            is.push_back(-is[mod() % i] * (mod() / i));
        }
        return is[n];
    }
    u64 m_val;
};
template<int id>
using modint64_dynamic = modint64<0, 0, id>;
template<typename mint>
bool millerRabin(u64 n, const Vec<u64>& as)
{
    auto d = n - 1;
    for (; (d & 1) == 0; d >>= 1) {}
    for (const u64 a : as) {
        if (n <= a) { break; }
        auto s = d;
        mint x = mint(a).pow(s);
        while (x.val() != 1 and x.val() != n - 1 and s != n - 1) {
            x *= x, s <<= 1;
        }
        if (x.val() != n - 1 and s % 2 == 0) { return false; }
    }
    return true;
}
bool isPrime(u64 n)
{
    using mint = modint_dynamic<873293817>;
    using mint64 = modint64_dynamic<828271328>;
    if (n == 1) { return false; }
    if ((n & 1) == 0) { return n == 2; }
    if (n < (1ULL << 30)) {
        mint::setMod(n);
        return millerRabin<mint>(n, {2, 7, 61});
    } else {
        mint64::setMod(n);
        return millerRabin<mint64>(n, {2, 325, 9375, 28178, 450775, 9780504});
    }
}
template<typename mint>
u64 pollardRho(u64 n)
{
    if (n % 2 == 0) { return 2; }
    if (isPrime(n)) { return n; }
    mint c;
    auto f = [&](const mint& x) { return x * x + c; };
    while (true) {
        mint x, y, ys, q = 1;
        y = rng64.val<u64>(0, n - 2) + 2;
        c = rng64.val<u64>(0, n - 2) + 2;
        u64 d = 1;
        constexpr u32 dk = 128;
        for (u32 r = 1; d == 1; r <<= 1) {
            x = y;
            for (u32 i = 0; i < r; i++) {
                y = f(y);
            }
            for (u32 k = 0; k < r and d == 1; k += dk) {
                ys = y;
                for (u32 i = 0; i < dk and i < r - k; i++) {
                    q *= x - (y = f(y));
                }
                d = std::gcd((u64)q.val(), n);
            }
        }
        if (d == n) {
            do {
                d = std::gcd(u64((x - (ys = f(ys))).val()), n);
            } while (d == 1);
        }
        if (d != n) { return d; }
    }
    return n;
}
Map<u64, int> primeFactors(u64 n)
{
    using mint = modint_dynamic<287687412>;
    using mint64 = modint64_dynamic<4832432>;
    Map<u64, int> ans;
    Fix([&](auto dfs, u64 x) -> void {
        while ((x & 1) == 0) {
            x >>= 1, ans[2]++;
        }
        if (x == 1) { return; }
        u64 p;
        if (x < (1ULL << 30)) {
            mint::setMod(x);
            p = pollardRho<mint>(x);
        } else {
            mint64::setMod(x);
            p = pollardRho<mint64>(x);
        }
        if (p == x) {
            ans[p]++;
            return;
        }
        dfs(p), dfs(x / p);
    })(n);
    return ans;
}
Vec<u64> divisors(const u64 n)
{
    const auto fs = primeFactors(n);
    Vec<u64> ds{1};
    for (const auto& [p, e] : fs) {
        u64 pe = p;
        const u32 dn = ds.size();
        for (i32 i = 0; i < e; i++, pe *= p) {
            for (u32 j = 0; j < dn; j++) {
                ds.push_back(ds[j] * pe);
            }
        }
    }
    return ds;
}
template<typename mint>
mint modNthRoot(mint A, i64 k)
{
    const i64 P = mint::mod();
    assert(P > 0);
    if (A == 0) { return 0; }
    if (k == 0) { return A; }
    const i64 g = std::gcd(P - 1, k);
    if (A.pow((P - 1) / g).val() != 1) { return 0; }
    A = A.pow(inverse(k / g, (P - 1) / g));
    if (g == 1) { return A; }
    const auto fs = primeFactors(g);
    for (const auto& [p, e] : fs) {
        i64 pe = 1;
        for (auto _temp_name_0 [[maybe_unused]] : rep(e)) {
            pe *= p;
        }
        i64 q = P - 1, Q = 0;
        while (q % p == 0) {
            q /= p, Q++;
        }
        auto [y, z] = extgcd(-q, pe);
        if (y <= 0) { y += pe, z += q; }
        mint X = A.pow(z);
        if ((int)Q == e) {
            A = X;
            continue;
        }
        mint Eraser = 1;
        const i64 h = (P - 1) / p;
        for (mint Z = 2;; Z += 1) {
            if (Z.pow(h) != 1) {
                Eraser = Z.pow(q);
                break;
            }
        }
        mint Error = A.pow(y * q);
        mint pEraser = Eraser;
        for (auto _temp_name_1 [[maybe_unused]] : rep(Q - 1)) {
            pEraser = pEraser.pow(p);
        }
        const mint ipEraser = pEraser.inv();
       // IntDict<i64, i64> memo;
UMap<i64,i64> memo;
        {
            const i64 M
                = std::max(1_i64, (i64)(std::sqrt(p) * std::sqrt(Q - e)));
            const i64 B = std::max(1_i64, ((i64)p - 1) / M);
            const mint ppEraser = pEraser.pow(B);
            mint prod = 1;
            for (i64 i = 0; i < p; i += B, prod *= ppEraser) {
                memo[prod.val()] = i;
            }
        }
        while (Error.val() != 1) {
            i64 l = 0;
            mint pError = Error;
            for (i64 i : rep(Q)) {
                const auto npError = pError.pow(p);
                if (npError == 1) {
                    l = Q - (i + 1);
                    break;
                }
                pError = npError;
            }
            i64 c = -1;
            {
                mint small = pError.inv();
                for (i64 j = 0;; j++, small *= ipEraser) {
                    if (memo.count(small.val())) {
                        const i64 i = memo[small.val()];
                        c = i + j;
                        break;
                    }
                }
            }
            auto pEraser2 = Eraser.pow(c);
            for (auto _temp_name_2 [[maybe_unused]] : rep(l - e)) {
                pEraser2 = pEraser2.pow(p);
            }
            X *= pEraser2, Error *= pEraser2.pow(pe);
        }
        A = X;
    }
    return A;
}
class Printer
{
public:
    Printer(Ostream& os = std::cout) : m_os{os}
    {
        m_os << std::fixed << std::setprecision(15);
    }
    template<typename... Args>
    int operator()(const Args&... args)
    {
        dump(args...);
        return 0;
    }
    template<typename... Args>
    int ln(const Args&... args)
    {
        dump(args...), m_os << '\n';
        return 0;
    }
    template<typename... Args>
    int el(const Args&... args)
    {
        dump(args...), m_os << std::endl;
        return 0;
    }
private:
    template<typename T>
    void dump(const T& v)
    {
        m_os << v;
    }
    template<typename T>
    void dump(const Vec<T>& vs)
    {
        for (const int i : rep(vs.size())) {
            m_os << (i ? " " : ""), dump(vs[i]);
        }
    }
    template<typename T>
    void dump(const Vec<Vec<T>>& vss)
    {
        for (const int i : rep(vss.size())) {
            m_os << (i ? "\n" : ""), dump(vss[i]);
        }
    }
    template<typename T, typename... Ts>
    int dump(const T& v, const Ts&... args)
    {
        dump(v), m_os << ' ', dump(args...);
        return 0;
    }
    Ostream& m_os;
};
Printer out;
class Scanner
{
public:
    Scanner(Istream& is = std::cin) : m_is{is}
    {
        m_is.tie(nullptr)->sync_with_stdio(false);
    }
    template<typename T>
    T val()
    {
        T v;
        return m_is >> v, v;
    }
    template<typename T>
    T val(T offset)
    {
        return val<T>() - offset;
    }
    template<typename T>
    Vec<T> vec(int n)
    {
        return genVec<T>(n, [&]() { return val<T>(); });
    }
    template<typename T>
    Vec<T> vec(int n, T offset)
    {
        return genVec<T>(n, [&]() { return val<T>(offset); });
    }
    template<typename T>
    Vec<Vec<T>> vvec(int n, int m)
    {
        return genVec<Vec<T>>(n, [&]() { return vec<T>(m); });
    }
    template<typename T>
    Vec<Vec<T>> vvec(int n, int m, const T offset)
    {
        return genVec<Vec<T>>(n, [&]() { return vec<T>(m, offset); });
    }
    template<typename... Args>
    auto tup()
    {
        return Tup<Args...>{val<Args>()...};
    }
    template<typename... Args>
    auto tup(const Args&... offsets)
    {
        return Tup<Args...>{val<Args>(offsets)...};
    }
private:
    Istream& m_is;
};
Scanner in;
using mint = modint64_dynamic<0>;
int main()
{
    const int T = in.val<int>();
    for (int t : rep(T)) {
        const auto [P, K, Y] = in.tup<u64, u64, u64>();
        if (P == 2) {
            out.ln(Y == 1 ? 1 : K == 0 ? -1 : 0);
            continue;
        }
        mint::setMod(P);
        const mint ans = modNthRoot(mint(Y), K);
        if (ans.pow(K) == Y) {
            out.ln(ans.val());
        } else {
            out.ln(-1);
        }
    }
    return 0;
}
0