cpp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: mod/sqrt.hpp

Depends on

Required by

Verified with

Code

#pragma once
#include "bit/ctz.hpp"
#include "util/rand.hpp"

template <class T>
optional<T> mod_sqrt(T a) {
  // Tonelli-Shanks
  if (T::mod() <= 2) return a;
  if (a == T(0)) return T(0);
  if (a.pow((T::mod() - 1) / 2) == -1) return nullopt;
  int s = ctz(T::mod() - 1);
  int q = (T::mod() - 1) >> s;
  T x = a.pow((q + 1) / 2);
  T b = rand32();
  while (b.pow((T::mod() - 1) / 2) != -1) b = rand32();
  b = b.pow(q);
  T ia = a.inv();
  s -= 2;
  for (T e = ia * x * x; e != 1; b *= b, s--) {
    if (e.pow(1 << s) != 1) x *= b, e = ia * x * x;
  }
  return x;
}
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "bit/ctz.hpp"

#pragma GCC target("bmi")

template <class T>
int ctz(T x) {
  if (!x) return sizeof(T) * 8;
  if constexpr (sizeof(T) <= sizeof(unsigned)) {
    return __builtin_ctz((unsigned)x);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
    return __builtin_ctzll((unsigned long long)x);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
    unsigned long long y = x;
    return y ? ctz(y)
             : sizeof(y) * 8 + ctz((unsigned long long)(x >> sizeof(y) * 8));
  }
}
#line 3 "util/seed.hpp"

auto seed() {
#if defined(LOCAL) && !defined(NO_FIX_SEED)
  return 314169265258979;
#endif
  return chrono::steady_clock::now().time_since_epoch().count();
}
#line 3 "util/rand.hpp"

uint32_t rand32() {
  static uint32_t x = seed();
  x ^= x << 13;
  x ^= x >> 17;
  x ^= x << 5;
  return x;
}

uint64_t rand64() {
  return uint64_t(rand32()) << 32 | rand32();
}
#line 4 "mod/sqrt.hpp"

template <class T>
optional<T> mod_sqrt(T a) {
  // Tonelli-Shanks
  if (T::mod() <= 2) return a;
  if (a == T(0)) return T(0);
  if (a.pow((T::mod() - 1) / 2) == -1) return nullopt;
  int s = ctz(T::mod() - 1);
  int q = (T::mod() - 1) >> s;
  T x = a.pow((q + 1) / 2);
  T b = rand32();
  while (b.pow((T::mod() - 1) / 2) != -1) b = rand32();
  b = b.pow(q);
  T ia = a.inv();
  s -= 2;
  for (T e = ia * x * x; e != 1; b *= b, s--) {
    if (e.pow(1 << s) != 1) x *= b, e = ia * x * x;
  }
  return x;
}
Back to top page