Asa's CP Library

a01sa01to's competitive programming library.

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

View the Project on GitHub a01sa01to/cp-library

:heavy_check_mark: tests/data-structure/modint/yukicoder-373.test.cpp

Depends on

Code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

#include "../../../cpp/library/data-structure/modint.hpp"
#define PROBLEM "https://yukicoder.me/problems/no/373"
using mint = asalib::ds::dynamic_modint<0>;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  mint::set_mod(d);
  cout << (mint(a) * b * c).val() << endl;
  return 0;
}
#line 1 "tests/data-structure/modint/yukicoder-373.test.cpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

#line 2 "library/data-structure/modint.hpp"

#line 5 "library/data-structure/modint.hpp"
#include <type_traits>
using namespace std;

#line 2 "library/_internal/modint-base.hpp"

#include <concepts>
#line 5 "library/_internal/modint-base.hpp"
using namespace std;

namespace asalib {
  namespace _internal {
    class modint_base {};

    template<typename T>
    concept is_modint = is_base_of_v<modint_base, T>;
  }  // namespace _internal
}  // namespace asalib
#line 2 "library/math/extgcd.hpp"

#line 4 "library/math/extgcd.hpp"
#include <optional>
#line 6 "library/math/extgcd.hpp"
using namespace std;

namespace asalib {
  namespace math {
    // Returns a pair (x, y) such that ax + by = c
    template<integral T>
    constexpr optional<pair<T, T>> extgcd(T a, T b, T c) {
      if (b == 0) {
        if (c % a != 0) return nullopt;
        return make_pair(c / a, 0);
      }
      auto res = extgcd(b, a % b, c);
      if (!res) return nullopt;
      auto [x, y] = *res;
      return make_pair(y, x - (a / b) * y);
    }
  }  // namespace math
}  // namespace asalib
#line 10 "library/data-structure/modint.hpp"

namespace asalib {
  namespace ds {
    template<unsigned int mod>
      requires(mod >= 1)
    class static_modint: private _internal::modint_base {
      using mint = static_modint;

      public:
      constexpr static_modint(): _val(0) {};

      template<integral T>
      constexpr static_modint(T x) {
        long long y = x % (long long) mod;
        if (y < 0) y += mod;
        _val = y;
      }

      friend constexpr mint operator+(const mint& l, const mint& r) { return mint(l) += r; }
      friend constexpr mint operator-(const mint& l, const mint& r) { return mint(l) -= r; }
      friend constexpr mint operator*(const mint& l, const mint& r) { return mint(l) *= r; }
      friend constexpr mint operator/(const mint& l, const mint& r) { return mint(l) /= r; }
      constexpr mint operator+() const { return *this; }
      constexpr mint operator-() const { return 0 - *this; }

      constexpr mint& operator+=(const mint& other) {
        _val += other._val;
        if (_val >= mod) _val -= mod;
        return *this;
      }
      constexpr mint& operator-=(const mint& other) {
        _val -= other._val;
        if (_val >= mod) _val += mod;
        return *this;
      }
      constexpr mint& operator*=(const mint& other) {
        unsigned long long z = _val;
        z *= other._val;
        _val = z % mod;
        return *this;
      }
      constexpr mint& operator/=(const mint& other) { return *this = *this * other.inv(); }

      constexpr mint& operator++() {
        _val++;
        if (_val == mod) _val = 0;
        return *this;
      }
      constexpr mint& operator--() {
        if (_val == 0) _val = mod;
        _val--;
        return *this;
      }
      constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
      }
      constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
      }

      friend constexpr bool operator==(const mint& l, const mint& r) { return l._val == r._val; }
      friend constexpr bool operator!=(const mint& l, const mint& r) { return !(l == r); }
      friend constexpr bool operator<(const mint& l, const mint& r) { return l._val < r._val; }

      template<integral T>
      constexpr mint pow(T x) const {
        assert(x >= 0);
        mint res = 1, base = *this;
        while (x) {
          if (x & 1) res *= base;
          base *= base;
          x >>= 1;
        }
        return res;
      }

      constexpr mint inv() const {
        if (is_prime_mod()) return pow(mod - 2);
        if (gcd(_val, mod) != 1) throw invalid_argument("Modular inverse does not exist");
        return mint(math::extgcd<long long>(_val, mod, 1).value().first);
      }

      constexpr unsigned int val() const { return _val; }

      private:
      unsigned int _val;
      static constexpr bool is_prime_mod() {
        for (unsigned int i = 2; i * i <= mod; ++i) {
          if (mod % i == 0) return false;
        }
        return true;
      }
    };

    template<unsigned int id>
    class dynamic_modint: private _internal::modint_base {
      using mint = dynamic_modint;

      public:
      constexpr dynamic_modint(): _val(0) {}

      template<integral T>
      constexpr dynamic_modint(T x) {
        assert(_mod >= 1);
        long long y = x % (long long) _mod;
        if (y < 0) y += _mod;
        _val = y;
      };

      friend constexpr mint operator+(const mint& l, const mint& r) { return mint(l) += r; }
      friend constexpr mint operator-(const mint& l, const mint& r) { return mint(l) -= r; }
      friend constexpr mint operator*(const mint& l, const mint& r) { return mint(l) *= r; }
      friend constexpr mint operator/(const mint& l, const mint& r) { return mint(l) /= r; }
      constexpr mint operator+() const { return *this; }
      constexpr mint operator-() const { return 0 - *this; }

      constexpr mint& operator+=(const mint& other) {
        _val += other._val;
        if (_val >= _mod) _val -= _mod;
        return *this;
      }
      constexpr mint& operator-=(const mint& other) {
        _val -= other._val;
        if (_val >= _mod) _val += _mod;
        return *this;
      }
      constexpr mint& operator*=(const mint& other) {
        unsigned long long z = _val;
        z *= other._val;
        _val = z % _mod;
        return *this;
      }
      constexpr mint& operator/=(const mint& other) { return *this = *this * other.inv(); }

      constexpr mint& operator++() {
        _val++;
        if (_val == _mod) _val = 0;
        return *this;
      }
      constexpr mint& operator--() {
        if (_val == 0) _val = _mod;
        _val--;
        return *this;
      }
      constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
      }
      constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
      }

      friend constexpr bool operator==(const mint& l, const mint& r) { return l._val == r._val && l._mod == r._mod; }
      friend constexpr bool operator!=(const mint& l, const mint& r) { return !(l == r); }
      friend constexpr bool operator<(const mint& l, const mint& r) { return l._val < r._val; }

      template<integral T>
      constexpr mint pow(T x) const {
        assert(x >= 0);
        mint res = 1, base = *this;
        while (x) {
          if (x & 1) res *= base;
          base *= base;
          x >>= 1;
        }
        return res;
      }

      constexpr mint inv() const {
        if (gcd(_val, _mod) != 1) throw invalid_argument("Modular inverse does not exist");
        return mint(math::extgcd<long long>(_val, _mod, 1).value().first);
      }

      constexpr unsigned int val() const { return _val; }
      constexpr static unsigned int mod() { return _mod; }
      constexpr static void set_mod(unsigned int mod) {
        assert(mod >= 1);
        _mod = mod;
      }

      private:
      unsigned int _val;
      static inline unsigned int _mod;
    };
  }  // namespace ds
}  // namespace asalib
#line 8 "tests/data-structure/modint/yukicoder-373.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/373"
using mint = asalib::ds::dynamic_modint<0>;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  mint::set_mod(d);
  cout << (mint(a) * b * c).val() << endl;
  return 0;
}
Back to top page