Submit Info #34291

Problem Lang User Status Time Memory
Kth Root(Mod) cpp-acl pachicobue AC 196 ms 17.10 MiB

ケース詳細
Name Status Time Memory
Tonelli-Shanks_worstcase_00 AC 195 ms 17.05 MiB
Tonelli-Shanks_worstcase_01 AC 196 ms 16.99 MiB
Tonelli-Shanks_worstcase_02 AC 195 ms 17.03 MiB
Tonelli-Shanks_worstcase_03 AC 194 ms 17.05 MiB
Tonelli-Shanks_worstcase_04 AC 195 ms 16.99 MiB
example_00 AC 8 ms 16.77 MiB
max_random_00 AC 166 ms 17.03 MiB
max_random_01 AC 163 ms 17.05 MiB
max_random_02 AC 164 ms 17.02 MiB
max_random_03 AC 164 ms 17.02 MiB
max_random_04 AC 166 ms 17.10 MiB
random_00 AC 46 ms 16.93 MiB
random_01 AC 105 ms 16.92 MiB
random_02 AC 141 ms 16.92 MiB
random_03 AC 46 ms 16.92 MiB
random_04 AC 34 ms 16.80 MiB
safe_prime_00 AC 153 ms 17.05 MiB
safe_prime_01 AC 151 ms 16.99 MiB
safe_prime_02 AC 152 ms 16.99 MiB
safe_prime_03 AC 153 ms 17.02 MiB
safe_prime_04 AC 153 ms 17.02 MiB
small_00 AC 76 ms 16.89 MiB

#include <bits/stdc++.h> using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using LL = __int128_t; using ULL = __uint128_t; template<typename T> using max_heap = std::priority_queue<T>; template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<typename K, typename V, int LG = 20> class hashmap { public: hashmap() = default; V& operator[](const K& k) { for (uint i = hash(k);; (i += 1) &= (N - 1)) { if (not m_used.test(i)) { m_keys[i] = k, m_used.set(i); return m_vals[i] = V{}; } if (m_keys[i] == k) { return m_vals[i]; } } } int count(const K& k) const { uint i = 0; for (i = hash(k); m_used.test(i) and m_keys[i] != k; (i += 1) &= (N - 1)) {} return m_used.test(i) and m_keys[i] == k; } private: static constexpr int N = 1 << LG; static constexpr ull r = 11995408973635179863ULL; static constexpr uint hash(const ull a) { return (a * r) >> (64 - LG); } std::bitset<N> m_used; K m_keys[N]; V m_vals[N]; }; template<typename T> constexpr T inverse(const T a, const T mod) { return a == 1 ? T{1} : ((a - inverse(mod % a, a)) * mod + 1) / a; } template<typename T> constexpr std::pair<T, T> extgcd(const T a, const T b) { if (a == 0) { return -1 / b; } if (b == 0) { return 1 / a; } const T x = inverse(a, b), y = (a * x - 1) / b; return {x, y}; } template<typename T, typename V> inline bool miller_rabin(const T& n, const std::vector<T>& as) { auto pow = [&](V a, T k) -> V { V ans = 1; for (; k > 0; (a *= a) %= V{n}, k >>= 1) { if (k & 1) { (ans *= a) %= V{n}; } } return ans; }; T d = n - 1; for (; (d & 1) == 0; d >>= 1) {} for (const T& a : as) { if (n <= a) { break; } T s = d; V x = pow(a, s); while (x != 1 and x != n - 1 and s != n - 1) { (x *= x) %= V(n); s *= 2; } if (x != n - 1 and s % 2 == 0) { return false; } } return true; } inline bool is_prime(const ull n) { if (n % 2 == 0) { return n == 2; } if (n < (1ULL << 32)) { return miller_rabin<uint, ull>((uint)n, std::vector<uint>{2, 7, 61}); } else { return miller_rabin<ull, __uint128_t>(n, std::vector<ull>{2, 325, 9375, 28178, 450775, 9780504}); } } template<typename T, typename V = T> T pollard_rho(const T n) { if (n % 2 == 0) { return 2; } if (is_prime(n)) { return n; } for (T c = 1; c < n; c++) { if (c == n - 2) { continue; } auto f = [&](const T x) -> T { return T((V(x) * V(x) + V(c)) % V(n)); }; T x = 2, y = 2, d = 1; while (d == 1) { x = f(x), y = f(f(y)); d = std::gcd(std::max(x, y) - std::min(x, y), n); } if (d != n) { return d; } } return n; } std::map<ull, int> prime_factors(const ull n_) { std::map<ull, int> ans; auto factor = [&](auto&& self, const ull n) -> void { if (n == 1) { return; } const ull p = (n < (1ULL << 32)) ? (ull)pollard_rho<uint, ull>((uint)n) : pollard_rho<ull, __uint128_t>(n); if (p == n) { ans[p]++; return; } self(self, p), self(self, n / p); }; factor(factor, n_); return ans; } template<typename mint> mint mod_nthroot(mint A, ull k) { const ull P = mint::mod; if (A == 0) { return 0; } if (k == 0) { return A; } const ull g = std::gcd(P - 1, k); if (A.pow((P - 1) / g)() != 1) { return 0; } A = A.pow(inverse<ULL>(k / g, (P - 1) / g)); if (g == 1) { return A; } const auto fs = prime_factors(g); for (const auto& [p, e] : fs) { ull pe = 1; for (int i = 0; i < e; i++) { pe *= p; } ull q = P - 1, Q = 0; while (q % p == 0) { q /= p, Q++; } const ull y = pe - inverse<ULL>(q, pe), z = ((ULL)y * q + 1) / (ULL)pe; mint X = A.pow(z); if ((int)Q == e) { A = X; continue; } mint Eraser = 1; const ull h = (P - 1) / p; for (mint Z = 2;; Z += 1) { if (Z.pow(h) != 1) { Eraser = Z.pow(q); break; } } mint Error = A.pow((ULL)y * q); mint pEraser = Eraser; for (ull i = 0; i < Q - 1; i++) { pEraser = pEraser.pow(p); } const mint ipEraser = pEraser.inv(); hashmap<ull, ull> memo; { const ull M = std::max(1ULL, (ull)(std::sqrt(p) * std::sqrt(Q - e))), B = std::max(1ULL, ((ull)p - 1) / M); const mint ppEraser = pEraser.pow(B); mint prod = 1; for (ull i = 0; i < p; i += B, prod *= ppEraser) { memo[prod()] = i; } } while (Error() != 1) { ull l = 0; mint pError = Error; for (ull i = 0; i < Q; i++) { const auto npError = pError.pow(p); if (npError == 1) { l = Q - (i + 1); break; } pError = npError; } ull c = -1; { mint small = pError.inv(); for (ull j = 0;; j++, small *= ipEraser) { if (memo.count(small())) { const ull i = memo[small()]; c = i + j; break; } } } auto pEraser2 = Eraser.pow(c); for (ull i = 0; i < l - e; i++) { pEraser2 = pEraser2.pow(p); } X *= pEraser2, Error *= pEraser2.pow(pe); } A = X; } return A; } struct modinfo { constexpr modinfo(const uint mod_, const uint root_, const uint max2p_) : mod{mod_}, root{root_}, max2p{max2p_} {} constexpr modinfo() : mod{}, root{}, max2p{} {} void set_mod(const uint mod_) { mod = mod_; } void set_root(const uint root_) { root = root_; } void set_max2p(const uint max2p_) { max2p = max2p_; } uint mod, root, max2p; }; template<const modinfo& info> class modint { public: static constexpr const uint& mod = info.mod; static constexpr const uint& root = info.root; static constexpr const uint& max2p = info.max2p; constexpr modint() : m_val{0} {} constexpr modint(const ll v) : m_val{normll(v)} {} constexpr modint(const modint& m) = default; constexpr void set_raw(const uint v) { m_val = v; } constexpr modint& operator=(const modint& m) { return m_val = m(), (*this); } constexpr modint& operator=(const ll v) { return m_val = normll(v), (*this); } constexpr modint operator+() const { return *this; } constexpr modint operator-() const { return modint{0} - (*this); } constexpr modint& operator+=(const modint& m) { return m_val = norm(m_val + m()), *this; } constexpr modint& operator-=(const modint& m) { return m_val = norm(m_val + mod - m()), *this; } constexpr modint& operator*=(const modint& m) { return m_val = normll((ll)m_val * (ll)m() % (ll)mod), *this; } constexpr modint& operator/=(const modint& m) { return *this *= m.inv(); } constexpr modint& operator+=(const ll val) { return *this += modint{val}; } constexpr modint& operator-=(const ll val) { return *this -= modint{val}; } constexpr modint& operator*=(const ll val) { return *this *= modint{val}; } constexpr modint& operator/=(const ll val) { return *this /= modint{val}; } constexpr modint operator+(const modint& m) const { return modint{*this} += m; } constexpr modint operator-(const modint& m) const { return modint{*this} -= m; } constexpr modint operator*(const modint& m) const { return modint{*this} *= m; } constexpr modint operator/(const modint& m) const { return modint{*this} /= m; } constexpr modint operator+(const ll v) const { return *this + modint{v}; } constexpr modint operator-(const ll v) const { return *this - modint{v}; } constexpr modint operator*(const ll v) const { return *this * modint{v}; } constexpr modint operator/(const ll v) const { return *this / modint{v}; } constexpr bool operator==(const modint& m) const { return m_val == m(); } constexpr bool operator!=(const modint& m) const { return not(*this == m); } constexpr friend modint operator+(const ll v, const modint& m) { return modint{v} + m; } constexpr friend modint operator-(const ll v, const modint& m) { return modint{v} - m; } constexpr friend modint operator*(const ll v, const modint& m) { return modint{v} * m; } constexpr friend modint operator/(const ll v, const modint& m) { return modint{v} / m; } friend std::istream& operator>>(std::istream& is, modint& m) { ll v; return is >> v, m = v, is; } friend std::ostream& operator<<(std::ostream& os, const modint& m) { return os << m(); } constexpr uint operator()() const { return m_val; } constexpr modint pow(ull n) const { modint ans = 1; for (modint x = *this; n > 0; n >>= 1, x *= x) { if (n & 1ULL) { ans *= x; } } return ans; } constexpr modint inv() const { return pow(mod - 2); } modint sinv() const { return sinv(m_val); } static modint fact(const uint n) { static std::vector<modint> fs{1, 1}; for (uint i = (uint)fs.size(); i <= n; i++) { fs.push_back(fs.back() * i); } return fs[n]; } static modint ifact(const uint n) { static std::vector<modint> ifs{1, 1}; for (uint i = (uint)ifs.size(); i <= n; i++) { ifs.push_back(ifs.back() * sinv(i)); } return ifs[n]; } static modint perm(const int n, const int k) { return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k); } static modint comb(const int n, const int k) { return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k) * ifact(k); } private: static constexpr uint norm(const uint x) { return x < mod ? x : x - mod; } static constexpr uint normll(const ll x) { return norm(uint(x % (ll)mod + (ll)mod)); } static modint sinv(const uint n) { static std::vector<modint> is{1, 1}; for (uint i = (uint)is.size(); i <= n; i++) { is.push_back(-is[mod % i] * (mod / i)); } return is[n]; } uint m_val; }; constexpr modinfo modinfo_1000000007 = {1000000007, 5, 1}; constexpr modinfo modinfo_998244353 = {998244353, 3, 23}; using modint_1000000007 = modint<modinfo_1000000007>; using modint_998244353 = modint<modinfo_998244353>; class printer { public: printer() { for (int i = 0; i < 10000; i++) { for (int j = i, t = 3; t >= 0; t--, j /= 10) { m_dict[i * 4 + t] = (j % 10) + '0'; } } } ~printer() { flush(); } template<typename... Args> int ln(const Args&... args) { return dump(args...), put_char('\n'), 0; } template<typename... Args> int el(const Args&... args) { return dump(args...), put_char('\n'), flush(), 0; } private: using ll = long long; using ull = unsigned long long; static constexpr ull TEN(const int d) { return d == 0 ? 1ULL : TEN(d - 1) * 10ULL; } void flush() { fwrite(m_memory, 1, m_tail, stdout), m_tail = 0; } void put_char(const char c) { m_memory[m_tail++] = c; } static constexpr int dn(const ull x) { return x < TEN(10) ? x < TEN(5) ? x < TEN(2) ? x < TEN(1) ? 1 : 2 : x < TEN(3) ? 3 : x < TEN(4) ? 4 : 5 : x < TEN(7) ? x < TEN(6) ? 6 : 7 : x < TEN(8) ? 8 : x < TEN(9) ? 9 : 10 : x < TEN(14) ? x < TEN(12) ? x < TEN(11) ? 11 : 12 : x < TEN(13) ? 13 : 14 : x < TEN(16) ? x < TEN(15) ? 15 : 16 : x < TEN(17) ? 17 : x < TEN(18) ? 18 : 19; } template<typename T> void dump(T v) { if (C - m_tail < 50) { flush(); } if (v < 0) { put_char('-'), v = -v; } const auto d = dn(v); int i = d - 4; for (i = d - 4; i >= 0; i -= 4, v /= 10000) { memcpy(m_memory + m_tail + i, m_dict + (v % 10000) * 4, 4); } memcpy(m_memory + m_tail, m_dict + v * 4 - i, i + 4); m_tail += d; } template<typename T> void dump(const std::vector<T>& vs) { for (int i = 0; i < (int)vs.size(); i++) { if (i > 0) { put_char(' '); } dump(vs[i]); } } template<typename T> void dump(const std::vector<std::vector<T>>& vss) { for (int i = 0; i < (int)vss.size(); i++) { if (i > 0) { put_char('\n'); } dump(vss[i]); } } template<typename T, typename... Args> void dump(const T& v, const Args&... args) { return dump(v), put_char(' '), dump(args...), void(0); } static constexpr int C = 1 << 18; int m_tail = 0; char m_memory[C]; char m_dict[10000 * 4]; } out; class scanner { public: scanner() {} template<typename T> T val() { if (m_tail - m_head < 40) { disk_read(); } char c = get_char(); const bool neg = (c == '-'); if (neg) { c = get_char(); } T ans = 0; while (c >= '0') { ans = ans * T{10} + (c - '0'); c = get_char(); } return (neg ? -ans : ans); } template<typename T> T val(const T offset) { return val<T>() - offset; } template<typename T> std::vector<T> vec(const int n) { return make_v<T>(n, [this]() { return val<T>(); }); } template<typename T> std::vector<T> vec(const int n, const T offset) { return make_v<T>(n, [this, offset]() { return val<T>(offset); }); } template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1) { return make_v<std::vector<T>>(n0, [this, n1]() { return vec<T>(n1); }); } template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset) { return make_v<std::vector<T>>(n0, [this, n1, offset]() { return vec<T>(n1, offset); }); } template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; } template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; } private: template<typename T, typename F> std::vector<T> make_v(const int n, F f) { std::vector<T> ans; for (int i = 0; i < n; i++) { ans.push_back(f()); } return ans; } char get_char() { return m_memory[m_head++]; } void disk_read() { std::copy(m_memory + m_head, m_memory + m_tail, m_memory); m_tail -= m_head, m_head = 0; m_tail += fread(m_memory + m_tail, 1, C - m_tail, stdin); } static constexpr int C = 1 << 18; int m_head = 0, m_tail = 0; char m_memory[C]; } in; constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); } constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; } constexpr ull ceil2(const ull v) { return 1ULL << clog(v); } constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; } constexpr bool ispow2(const ull v) { return (v & (v - 1)) == 0; } constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; } template<typename Rng> class rng_base { public: using result_type = typename Rng::result_type; static constexpr result_type min() { return Rng::min(); } static constexpr result_type max() { return Rng::max(); } rng_base() : rng_base(std::random_device{}()) {} rng_base(const std::random_device::result_type seed) : m_rng(seed) {} ~rng_base() = default; result_type operator()() { return m_rng(); } result_type val(const result_type max = std::numeric_limits<result_type>::max()) { if (max == std::numeric_limits<result_type>::max()) { return m_rng(); } const result_type mask = ceil2(max + 1) - 1; while (true) { const result_type ans = m_rng() & mask; if (ans <= max) { return ans; } } } template<typename T> T val(const T min, const T max) { return min + T(val(max - min)); } operator bool() { return val<bool>(0, 1); } template<typename T> std::pair<T, T> pair(const T min, const T max) { return std::minmax(val<T>(min, max), val<T>(min, max)); } template<typename T> std::vector<T> vec(const int n, const T min, const T max) { std::vector<T> vs(n); for (auto& v : vs) { v = val<T>(min, max); } return vs; } private: Rng m_rng; }; rng_base<std::mt19937> rng; rng_base<std::mt19937_64> rng64; modinfo info; using mint = modint<info>; int main() { const int T = in.val<int>(); for (int t = 0; t < T; t++) { const auto [K, Y, P] = in.tup<uint, uint, uint>(); info.set_mod(P); const mint ans = mod_nthroot(mint{Y}, K); if (ans.pow(K) == Y) { out.ln(ans()); } else { out.ln(-1); } } return 0; }