kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:x: test/yosupo-judge/sqrt_mod/main.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/sqrt_mod"

#include <iostream>
#include "Mylib/IO/input_tuples.cpp"
#include "Mylib/Number/Mod/mod_sqrt.cpp"

namespace hl = haar_lib;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int T;
  std::cin >> T;
  for (auto [Y, P] : hl::input_tuples<int64_t, int64_t>(T)) {
    std::cout << hl::mod_sqrt(Y, P).value_or(-1) << "\n";
  }
  return 0;
}
#line 1 "test/yosupo-judge/sqrt_mod/main.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/sqrt_mod"

#include <iostream>
#line 2 "Mylib/IO/input_tuples.cpp"
#include <initializer_list>
#line 4 "Mylib/IO/input_tuples.cpp"
#include <tuple>
#include <utility>
#include <vector>
#line 6 "Mylib/IO/input_tuple.cpp"

namespace haar_lib {
  template <typename T, size_t... I>
  static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) {
    (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...};
  }

  template <typename T, typename U>
  std::istream &operator>>(std::istream &s, std::pair<T, U> &value) {
    s >> value.first >> value.second;
    return s;
  }

  template <typename... Args>
  std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) {
    input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>());
    return s;
  }
}  // namespace haar_lib
#line 8 "Mylib/IO/input_tuples.cpp"

namespace haar_lib {
  template <typename... Args>
  class InputTuples {
    struct iter {
      using value_type = std::tuple<Args...>;
      value_type value;
      bool fetched = false;
      int N, c = 0;

      value_type operator*() {
        if (not fetched) {
          std::cin >> value;
        }
        return value;
      }

      void operator++() {
        ++c;
        fetched = false;
      }

      bool operator!=(iter &) const {
        return c < N;
      }

      iter(int N) : N(N) {}
    };

    int N;

  public:
    InputTuples(int N) : N(N) {}

    iter begin() const { return iter(N); }
    iter end() const { return iter(N); }
  };

  template <typename... Args>
  auto input_tuples(int N) {
    return InputTuples<Args...>(N);
  }
}  // namespace haar_lib
#line 2 "Mylib/Number/Mod/mod_sqrt.cpp"
#include <optional>
#include <random>
#line 2 "Mylib/Number/Mod/mod_pow.cpp"
#include <cstdint>

namespace haar_lib {
  constexpr int64_t mod_pow(int64_t n, int64_t p, int64_t m) {
    int64_t ret = 1;
    while (p > 0) {
      if (p & 1) (ret *= n) %= m;
      (n *= n) %= m;
      p >>= 1;
    }
    return ret;
  }
}  // namespace haar_lib
#line 5 "Mylib/Number/Mod/mod_sqrt.cpp"

namespace haar_lib {
  std::optional<int64_t> mod_sqrt(int64_t a, int64_t p) {
    if (p == 2) return a % 2;
    if (a == 0) return 0;

    int64_t b = mod_pow(a, (p - 1) / 2, p);

    if (b == p - 1) return {};
    if (p % 4 == 3) return mod_pow(a, (p + 1) / 4, p);

    int64_t q = p - 1, s = 0;
    while (q % 2 == 0) {
      q /= 2;
      s += 1;
    }

    static std::mt19937_64 rand(time(0));
    std::uniform_int_distribution<> dist(0, p - 1);

    int64_t z;
    while (1) {
      z = dist(rand);
      if (mod_pow(z, (p - 1) / 2, p) == p - 1) break;
    }

    int64_t m = s;
    int64_t c = mod_pow(z, q, p);
    int64_t t = mod_pow(a, q, p);
    int64_t r = mod_pow(a, (q + 1) / 2, p);

    while (1) {
      if (t == 0) return 0;
      if (t == 1) return r;

      int i = 1;
      for (int64_t T = t; i < m; ++i) {
        (T *= T) %= p;
        if (T == 1) break;
      }

      int64_t b = mod_pow(c, 1LL << (m - i - 1), p);

      m = i;
      c = b * b % p;
      (t *= b * b % p) %= p;
      (r *= b) %= p;
    }
  }
}  // namespace haar_lib
#line 6 "test/yosupo-judge/sqrt_mod/main.test.cpp"

namespace hl = haar_lib;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int T;
  std::cin >> T;
  for (auto [Y, P] : hl::input_tuples<int64_t, int64_t>(T)) {
    std::cout << hl::mod_sqrt(Y, P).value_or(-1) << "\n";
  }
  return 0;
}
Back to top page