sotanishy's code snippets for competitive programming
View the Project on GitHub sotanishy/cp-library-cpp
#define PROBLEM "https://judge.yosupo.jp/problem/montmort_number_mod" #include "../../math/arbitrary_modint.hpp" #include "../../math/montmort.cpp" #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; using mint = ArbitraryModint; mint::set_mod(M); vector<mint> ans = montmort_table<mint>(N); for (int i = 1; i <= N; ++i) cout << ans[i] << (i < N ? " " : "\n"); }
#line 1 "test/yosupo/montmort_number_mod.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/montmort_number_mod" #line 2 "math/arbitrary_modint.hpp" #include <algorithm> #include <cassert> #include <iostream> /** * @brief Arbitrary Mod int */ class ArbitraryModint { using mint = ArbitraryModint; public: static int& mod() { static int mod_ = 1; return mod_; } static void set_mod(int mod_) { assert(mod_ > 0); mod() = mod_; } ArbitraryModint(long long y = 0) : x(y >= 0 ? y % mod() : (y % mod() + mod()) % mod()) {} int val() const { return x; } mint& operator+=(const mint& r) { if ((x += r.x) >= mod()) x -= mod(); return *this; } mint& operator-=(const mint& r) { if ((x += mod() - r.x) >= mod()) x -= mod(); return *this; } mint& operator*=(const mint& r) { x = static_cast<int>(1LL * x * r.x % mod()); return *this; } mint& operator/=(const mint& r) { return *this *= r.inv(); } bool operator==(const mint& r) const { return x == r.x; } mint operator+() const { return *this; } mint operator-() const { return mint(-x); } friend mint operator+(const mint& l, const mint& r) { return mint(l) += r; } friend mint operator-(const mint& l, const mint& r) { return mint(l) -= r; } friend mint operator*(const mint& l, const mint& r) { return mint(l) *= r; } friend mint operator/(const mint& l, const mint& r) { return mint(l) /= r; } mint inv() const { int a = x, b = mod(), u = 1, v = 0; while (b > 0) { int t = a / b; std::swap(a -= t * b, b); std::swap(u -= t * v, v); } return mint(u); } mint pow(long long n) const { mint ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend std::ostream& operator<<(std::ostream& os, const mint& r) { return os << r.x; } friend std::istream& operator>>(std::istream& is, mint& r) { long long t; is >> t; r = mint(t); return is; } private: int x; }; #line 2 "math/montmort.cpp" #include <vector> /* * @brief Monmort Number */ template <typename T> std::vector<T> montmort_table(int n) { std::vector<T> ret(n + 1); if (n == 1) return ret; ret[2] = 1; for (int i = 3; i <= n; ++i) { ret[i] = (ret[i - 1] + ret[i - 2]) * (i - 1); } return ret; } #line 5 "test/yosupo/montmort_number_mod.test.cpp" #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; using mint = ArbitraryModint; mint::set_mod(M); vector<mint> ans = montmort_table<mint>(N); for (int i = 1; i <= N; ++i) cout << ans[i] << (i < N ? " " : "\n"); }