CompetitiveProgrammingCpp

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

View the Project on GitHub

:heavy_check_mark: Test/Math/Math.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/117"

#include <iostream>

#include <string>


// begin:tag includes

#include "../../Library/Math/ModInt.hpp"

#include "../../Library/Range/istream.hpp"

// end:tag includes


auto parse(const std::string s) {
  long long a = 0, b = 0;
  bool isa = true;
  for (unsigned int i = 2; i < s.size() - 1; ++i) {
    if (s[i] == ',') {
      isa = false;
      continue;
    }
    auto& x = ((isa) ? a : b);
    x = 10 * x + (s[i] - '0');
  }
  return std::make_pair(a, b);
}

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

  auto [t] = mtd::io::in<int>();

  constexpr long long MOD = 1e9 + 7;
  using mint = mtd::ModInt<MOD>;
  auto math = mtd::Math<mint>();
  for (auto [s] : mtd::views::istream<std::string>(t)) {
    auto [n, k] = parse(s);
    if (s[0] == 'C') {
      std::cout << math.comb(n, k) << std::endl;
    } else if (s[0] == 'P') {
      std::cout << math.perm(n, k) << std::endl;
    } else {
      std::cout << math.comb(std::max(0LL, n + k - 1), k) << std::endl;
    }
  }
}
#line 1 "Test/Math/Math.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/117"

#include <iostream>

#include <string>


// begin:tag includes

#line 2 "Library/Math/ModInt.hpp"

#line 4 "Library/Math/ModInt.hpp"
#include <iterator>

#line 2 "Library/Math/Math.hpp"

#include <cmath>

#include <numeric>

#include <optional>

#include <ranges>

#include <unordered_map>

#include <vector>


#line 2 "Library/Math/EuclideanAlgorithm.hpp"

#line 5 "Library/Math/EuclideanAlgorithm.hpp"
#include <tuple>


namespace mtd {

  class EuclideanAlgorithm {
    using T = long long;

    // 大きすぎるとオーバーフローしてしまう

    const static inline T m_mx = 1e9;

    const T m_a;
    const T m_b;
    const T m_c;

    T m_gcd;
    T m_x;
    T m_y;

    auto excludedEuclidAlgorithm(T a, T b) -> std::tuple<T, T, T> {
      if (a < 0) {
        auto [g, x, y] = excludedEuclidAlgorithm(-a, -b);
        return {g, -x, -y};
      }
      if (b == 0) { return {a, 1, 0}; }
      auto [g, y, x] = excludedEuclidAlgorithm(b, a % b);
      y -= a / b * x;
      return {g, x, y};
    }

    auto kRange(T x, T b, T l) const -> std::pair<T, T> {
      // x + b * k >= l を満たす k の範囲を求める

      T xd = (l - x);
      if (b == 0 && x >= l) { return {-m_mx, m_mx}; }
      if (b == 0 && x < l) { return {m_mx, -m_mx}; }
      if (b > 0 && xd < 0) { return {xd / b, m_mx}; }
      if (b > 0 && xd >= 0) { return {(xd + b - 1) / b, m_mx}; }
      if (b < 0 && xd < 0) { return {-m_mx, (-xd) / (-b)}; }
      if (b < 0 && xd >= 0) { return {-m_mx, -(xd - b - 1) / (-b)}; }
      return {m_mx, -m_mx};
    }

  public:
    auto debug() const {
      std::cout << m_a << " * " << m_x << " + " << m_b << " * " << m_y << " = "
                << m_c << std::endl;
      std::cout << "calc: " << m_a * m_x + m_b * m_y << " = " << m_c
                << std::endl;
    }

    EuclideanAlgorithm(T a, T b, T c) : m_a(a), m_b(b), m_c(c) {
      if (a == 0 && b == 0) { throw std::runtime_error(""); }
      auto [g, x, y] = excludedEuclidAlgorithm(a, b);
      if (c % g > 0) {
        throw std::runtime_error(
            "There is no solution to the equation. c must be divisible by "
            "gcd(a,b).");
      }
      m_gcd = g;
      m_x = c / g * x;
      m_y = c / g * y;
    }
    EuclideanAlgorithm(T a, T b) : EuclideanAlgorithm(a, b, std::gcd(a, b)) {}

    auto gcd() const { return m_gcd; }
    auto get(T x, T y) const { return m_a * x + m_b * y; }
    auto get(T k) const -> std::pair<T, T> {
      if (m_b == 0) { return {m_x, m_y - k}; }
      if (m_a == 0) { return {m_x + k, m_y}; }
      return {m_x + m_b * k, m_y - m_a * k};
    }
    // x>=x_lとなるようなkの範囲

    auto getMinX(T x_l = 0) const -> std::pair<T, T> {
      return kRange(m_x, m_b, x_l);
    }
    // y>=y_lとなるようなkの範囲

    auto getMinY(T y_l = 0) const -> std::pair<T, T> {
      return kRange(m_y, -1 * m_a, y_l);
    }
    // x>=x_l, y>=y_lとなるようなkの範囲

    auto getMin(T x_l = 0, T y_l = 0) const -> std::pair<T, T> {
      auto [xl, xr] = getMinX(x_l);
      auto [yl, yr] = getMinY(y_l);
      return {std::max(xl, yl), std::min(xr, yr)};
    }
  };

}  // namespace mtd

#line 11 "Library/Math/Math.hpp"

namespace mtd {
  template <class T>
  class Math {
    const std::vector<T> m_fac;
    const std::vector<T> m_finv;

    auto constructFac(int s) {
      std::vector<T> fac(s);
      fac[0] = fac[1] = 1;
      for (long long i = 2; i < s; ++i) { fac[i] = fac[i - 1] * i; }
      return fac;
    }
    auto constructInv(int s) {
      std::vector<T> finv(s);
      finv[s - 1] = 1 / m_fac[s - 1];
      for (long long i = s - 2; i >= 0; --i) {
        finv[i] = finv[i + 1] * (i + 1);
      }
      return finv;
    }

  public:
    constexpr Math(int size = 3 * static_cast<int>(1e6))
        : m_fac(constructFac(size)), m_finv(constructInv(size)) {}

    /* O(log b) */
    static constexpr T pow(T a, long long b) {
      T ans = 1;
      while (b > 0) {
        if (b & 1) { ans *= a; }
        b >>= 1;
        a *= a;
      }
      return ans;
    }

    /* O(log mod) */
    template <class S>
    static constexpr std::optional<long long> log(S x, S y, S mod) {
      x %= mod;
      y %= mod;

      if (mod == 1) { return 0; }
      if (x == 0 && y == 0) { return 1; }
      if (x == 0 && y == 1) { return 0; }
      if (x == 0) { return std::nullopt; }
      if (y == 1) { return 0; }

      if (auto g = std::gcd(x, mod); g > 1) {
        if (y % g) { return std::nullopt; }
        auto nx = x / g;
        auto nmod = mod / g;
        auto ea = mtd::EuclideanAlgorithm(nx, -nmod, 1);
        auto [t, _] = ea.getMinX();
        auto [nx_inv, __] = ea.get(t);
        nx_inv %= nmod;
        if (auto ans = log(x, y / g * nx_inv, nmod); ans) {
          return ans.value() + 1;
        } else {
          return ans;
        }
      }

      auto s = static_cast<S>(std::sqrt(mod));
      S xe = y;
      std::unordered_map<S, S> map;
      map.reserve(s);
      for (auto i : std::views::iota(0, s)) {
        (xe *= x) %= mod;
        map[xe] = i + 1;
      }

      S xs = 1;
      for ([[maybe_unused]] auto _ : std::views::iota(0, s)) {
        (xs *= x) %= mod;
      }
      S xse = 1;
      for (auto i : std::views::iota(0, mod / s + 5)) {
        (xse *= xs) %= mod;
        if (map.contains(xse)) { return s * (i + 1) - map[xse]; }
      }
      return std::nullopt;
    }
    constexpr std::optional<long long> log(long long x,
                                           long long y) requires requires {
      typename T::value_type;
      T::mod();
    }
    { return log(x, y, T::mod()); }

    constexpr auto fact(int n) const { return (n < 0) ? 0 : m_fac[n]; }
    constexpr auto factInv(int n) const { return (n < 0 ? 0 : m_finv[n]); }
    constexpr auto comb(int n, int r) const {
      return fact(n) * factInv(r) * factInv(n - r);
    }
    constexpr auto perm(int n, int r) const { return fact(n) * factInv(n - r); }
  };
}  // namespace mtd

#line 7 "Library/Math/ModInt.hpp"

namespace mtd {

  template <int MOD, class T = long long>
  class ModInt {
  public:
    using value_type = T;
    T x;

    constexpr ModInt(T _x) : x(_x >= 0 ? _x % MOD : MOD + (_x % MOD)) {}
    constexpr ModInt() : ModInt(0) {}

    // 四則演算
    constexpr auto& operator+=(const ModInt<MOD, T>& m) {
      x += m.x;
      if (x >= MOD) { x -= MOD; }
      return *this;
    }
    constexpr auto& operator-=(const ModInt<MOD, T>& m) {
      x -= m.x;
      if (x < 0) { x += MOD; }
      return *this;
    }
    constexpr auto& operator*=(const ModInt<MOD, T>& m) {
      x *= m.x;
      if (x >= MOD) { x %= MOD; }
      return *this;
    }
    constexpr auto& operator/=(const ModInt<MOD, T>& m) {
      x *= mtd::Math<ModInt<MOD, T>>::pow(m.x, MOD - 2).x;
      if (x >= MOD) { x %= MOD; }
      return *this;
    }

    constexpr auto operator+(const ModInt<MOD, T>& m) const {
      auto t = *this;
      t += m;
      return t;
    }
    constexpr auto operator-(const ModInt<MOD, T>& m) const {
      auto t = *this;
      t -= m;
      return t;
    }
    constexpr auto operator*(const ModInt<MOD, T>& m) const {
      auto t = *this;
      t *= m;
      return t;
    }
    constexpr auto operator/(const ModInt<MOD, T>& m) const {
      auto t = *this;
      t /= m;
      return t;
    }

    constexpr auto& operator+=(const T& t) {
      return *this += ModInt<MOD, T>(t);
    }
    constexpr auto& operator-=(const T& t) {
      return *this -= ModInt<MOD, T>(t);
    }
    constexpr auto& operator*=(const T& n) {
      return *this *= ModInt<MOD, T>(n);
    }
    constexpr auto& operator/=(const T& n) {
      return *this /= ModInt<MOD, T>(n);
    }
    constexpr auto operator+(const T& t) const {
      return *this + ModInt<MOD, T>(t);
    }
    constexpr auto operator-(const T& t) const {
      return *this - ModInt<MOD, T>(t);
    }
    constexpr auto operator*(const T& t) const {
      return *this * ModInt<MOD, T>(t);
    }
    constexpr auto operator/(const T& t) const {
      return *this / ModInt<MOD, T>(t);
    }
    constexpr friend auto operator+(const T& t, const ModInt<MOD, T>& m) {
      return m + t;
    }
    constexpr friend auto operator-(const T& t, const ModInt<MOD, T>& m) {
      return -m + t;
    }
    constexpr friend auto operator*(const T& t, const ModInt<MOD, T>& m) {
      return m * t;
    }
    constexpr friend auto operator/(const T& t, const ModInt<MOD, T>& m) {
      return ModInt<MOD, T>(1) / m * t;
    }

    // 単項演算
    constexpr auto operator-() const { return ModInt<MOD, T>(0 - x); }

    // 比較演算
    constexpr auto operator!=(const ModInt<MOD, T>& m) const {
      return x != m.x;
    }
    constexpr auto operator==(const ModInt<MOD, T>& m) const {
      return !(x != m.x);
    }

    // 入出力
    constexpr friend std::ostream& operator<<(std::ostream& os,
                                              const ModInt<MOD, T>& m) {
      return os << m.x;
    }
    constexpr friend std::istream& operator>>(std::istream& is,
                                              ModInt<MOD, T>& m) {
      return is >> m.x;
    }

    constexpr auto val() const { return x; }
    static constexpr auto mod() { return MOD; }
  };

}  // namespace mtd
#line 2 "Library/Range/istream.hpp"

#line 4 "Library/Range/istream.hpp"

#line 2 "Library/Utility/io.hpp"

#line 5 "Library/Utility/io.hpp"
#include <type_traits>
#line 7 "Library/Utility/io.hpp"

#line 2 "Library/Utility/Tuple.hpp"

#include <functional>

namespace mtd {
  namespace util {
    template <class F, class T>
    constexpr auto tuple_transform(F&& f, T&& t) {
      return std::apply(
          [&]<class... Ts>(Ts&&... elems) {
            return std::tuple<std::invoke_result_t<F&, Ts>...>(
                std::invoke(f, std::forward<Ts>(elems))...);
          },
          std::forward<T>(t));
    }
    template <class F, class T>
    constexpr auto tuple_for_each(F&& f, T&& t) {
      std::apply(
          [&]<class... Ts>(Ts&&... elems) {
            (std::invoke(f, std::forward<Ts>(elems)), ...);
          },
          std::forward<T>(t));
    }
  }  // namespace util
}  // namespace mtd
#line 9 "Library/Utility/io.hpp"

namespace mtd {
  namespace io {

    namespace __details {
      template <typename T>
      concept is_vec = std::same_as<
          T, std::vector<typename T::value_type, typename T::allocator_type>>;
      template <typename T>
      concept is_mat = is_vec<T> && is_vec<typename T::value_type>;

    }  // namespace __details

    template <class T>
    constexpr auto _input() {
      T x;
      std::cin >> x;
      return x;
    }
    template <typename T>
    requires requires { typename std::tuple_size<T>::type; }
    constexpr auto _input() {
      T x;
      util::tuple_for_each([](auto&& i) { std::cin >> i; }, x);
      return x;
    }
    template <__details::is_vec T>
    constexpr auto _input(int n) {
      std::vector<typename T::value_type> v;
      v.reserve(n);
      for (auto i : std::views::iota(0, n)) {
        v.emplace_back(_input<typename T::value_type>());
      }
      return v;
    }
    template <__details::is_mat T>
    constexpr auto _input(int h, int w) {
      T mat;
      mat.reserve(h);
      for (auto i : std::views::iota(0, h)) {
        mat.emplace_back(_input<typename T::value_type>(w));
      }
      return mat;
    }

    template <int N, class Tuple, class T, class... Args, class... Sizes>
    constexpr auto _tuple_input(Tuple& t, Sizes... sizes);
    template <int N, class Tuple, __details::is_vec T, class... Args,
              class Size, class... Sizes>
    constexpr auto _tuple_input(Tuple& t, Size size, Sizes... sizes);
    template <int N, class Tuple, __details::is_mat T, class... Args,
              class Size, class... Sizes>
    constexpr auto _tuple_input(Tuple& t, Size size_h, Size size_w,
                                Sizes... sizes);

    template <int N, class Tuple, class T, class... Args, class... Sizes>
    constexpr auto _tuple_input(Tuple& t, Sizes... sizes) {
      std::get<N>(t) = _input<T>();
      if constexpr (sizeof...(Args) > 0) {
        _tuple_input<N + 1, Tuple, Args...>(t, sizes...);
      }
    }
    template <int N, class Tuple, __details::is_vec T, class... Args,
              class Size, class... Sizes>
    constexpr auto _tuple_input(Tuple& t, Size size, Sizes... sizes) {
      std::get<N>(t) = _input<T>(size);
      if constexpr (sizeof...(Args) > 0) {
        _tuple_input<N + 1, Tuple, Args...>(t, sizes...);
      }
    }
    template <int N, class Tuple, __details::is_mat T, class... Args,
              class Size, class... Sizes>
    constexpr auto _tuple_input(Tuple& t, Size size_h, Size size_w,
                                Sizes... sizes) {
      std::get<N>(t) = _input<T>(size_h, size_w);
      if constexpr (sizeof...(Args) > 0) {
        _tuple_input<N + 1, Tuple, Args...>(t, sizes...);
      }
    }

    template <class... Args, class... Sizes>
    requires(std::convertible_to<Sizes, size_t>&&...) constexpr auto in(
        Sizes... sizes) {
      auto base = std::tuple<Args...>();
      _tuple_input<0, decltype(base), Args...>(base, sizes...);
      return base;
    }

  }  // namespace io

}  // namespace mtd
#line 6 "Library/Range/istream.hpp"

namespace mtd {
  namespace ranges {

    constexpr int _inf = 1e9;

    template <class... Args>
    struct istream_view
        : public std::ranges::view_interface<istream_view<Args...>> {
      class iterator {
        int count;
        std::tuple<Args...> val;

      public:
        using difference_type = int;
        using value_type = decltype(val);
        using iterator_concept = std::input_iterator_tag;

        constexpr iterator() = default;
        constexpr explicit iterator(int _count) : count(_count) {
          operator++();
        }

        constexpr auto operator*() const { return val; }
        constexpr auto& operator++() {
          --count;
          if (count >= 0) { val = io::in<Args...>(); }
          return *this;
        }
        constexpr auto operator++(int) { return ++*this; }

        constexpr auto operator==(const iterator& s) const {
          return count == s.count;
        }
        constexpr auto operator==(std::default_sentinel_t) const {
          return count < 0 || std::cin.eof() || std::cin.fail() ||
                 std::cin.bad();
        }
        constexpr friend auto operator==(std::default_sentinel_t s,
                                         const iterator& li) {
          return li == s;
        }
      };

      int count;

    public:
      constexpr explicit istream_view(int _count) : count(_count) {}
      constexpr explicit istream_view() : istream_view(_inf) {}
      constexpr auto begin() const { return iterator(count); }
      constexpr auto end() const { return std::default_sentinel; }
    };
  }  // namespace ranges

  namespace views {
    namespace __detail {
      template <typename... _Args>
      concept __can_istream_view = requires {
        ranges::istream_view(std::declval<_Args>()...);
      };
    }  // namespace __detail

    template <class... Args>
    struct _Istream {
      template <class... _Tp>
      requires __detail::__can_istream_view<_Tp...>
      constexpr auto operator() [[nodiscard]] (_Tp&&... __e) const {
        return ranges::istream_view<Args...>(std::forward<_Tp>(__e)...);
      }
    };

    template <class... Args>
    inline constexpr _Istream<Args...> istream{};
  }  // namespace views

}  // namespace mtd
#line 9 "Test/Math/Math.test.cpp"
// end:tag includes


auto parse(const std::string s) {
  long long a = 0, b = 0;
  bool isa = true;
  for (unsigned int i = 2; i < s.size() - 1; ++i) {
    if (s[i] == ',') {
      isa = false;
      continue;
    }
    auto& x = ((isa) ? a : b);
    x = 10 * x + (s[i] - '0');
  }
  return std::make_pair(a, b);
}

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

  auto [t] = mtd::io::in<int>();

  constexpr long long MOD = 1e9 + 7;
  using mint = mtd::ModInt<MOD>;
  auto math = mtd::Math<mint>();
  for (auto [s] : mtd::views::istream<std::string>(t)) {
    auto [n, k] = parse(s);
    if (s[0] == 'C') {
      std::cout << math.comb(n, k) << std::endl;
    } else if (s[0] == 'P') {
      std::cout << math.perm(n, k) << std::endl;
    } else {
      std::cout << math.comb(std::max(0LL, n + k - 1), k) << std::endl;
    }
  }
}
Back to top page