This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/502
#include <iostream>
#include <vector>
#include "../../math/combinatorics/factorial.hpp"
#include "../../math/combinatorics/montgomery-mod-int.hpp"
#include "../../math/fft/arbitrary-mod-convolution.hpp"
using namespace std;
using mint = modint1000000007;
int main() {
int N;
cin >> N;
ArbitraryModConvolution<mint> fft;
auto f = [&](vector<mint>& a, vector<mint>& b) { return fft.multiply(a, b); };
cout << factorial<mint>(N, f) << "\n";
}
#line 1 "test/verify/yukicoder-502.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/502
#include <iostream>
#include <vector>
#line 2 "math/combinatorics/factorial.hpp"
#include <algorithm>
#include <cstdint>
#include <iterator>
#line 7 "math/combinatorics/factorial.hpp"
#line 2 "math/combinatorics/sample-point-shift.hpp"
#line 5 "math/combinatorics/sample-point-shift.hpp"
#line 2 "math/combinatorics/enumeration.hpp"
#line 4 "math/combinatorics/enumeration.hpp"
#include <cstddef>
#line 6 "math/combinatorics/enumeration.hpp"
/**
* @brief Enumeration(組み合わせ)
*/
template <typename T>
struct Enumeration {
private:
static std::vector<T> _fact, _finv, _inv;
inline static void expand(std::size_t sz) {
if (_fact.size() < sz + 1) {
int pre_sz = std::max(1, (int)_fact.size());
_fact.resize(sz + 1, T(1));
_finv.resize(sz + 1, T(1));
_inv.resize(sz + 1, T(1));
for (int i = pre_sz; i <= (int)sz; i++) {
_fact[i] = _fact[i - 1] * T(i);
}
_finv[sz] = T(1) / _fact[sz];
for (int i = (int)sz - 1; i >= pre_sz; i--) {
_finv[i] = _finv[i + 1] * T(i + 1);
}
for (int i = pre_sz; i <= (int)sz; i++) {
_inv[i] = _finv[i] * _fact[i - 1];
}
}
}
public:
explicit Enumeration(std::size_t sz = 0) { expand(sz); }
static inline T fact(int k) {
expand(k);
return _fact[k];
}
static inline T finv(int k) {
expand(k);
return _finv[k];
}
static inline T inv(int k) {
expand(k);
return _inv[k];
}
static T P(int n, int r) {
if (r < 0 || n < r) return 0;
return fact(n) * finv(n - r);
}
static T C(int p, int q) {
if (q < 0 || p < q) return 0;
return fact(p) * finv(q) * finv(p - q);
}
static T H(int n, int r) {
if (n < 0 || r < 0) return 0;
return r == 0 ? 1 : C(n + r - 1, r);
}
};
template <typename T>
std::vector<T> Enumeration<T>::_fact = std::vector<T>();
template <typename T>
std::vector<T> Enumeration<T>::_finv = std::vector<T>();
template <typename T>
std::vector<T> Enumeration<T>::_inv = std::vector<T>();
#line 7 "math/combinatorics/sample-point-shift.hpp"
/**
* @brief Sample Point Shift(標本点シフト)
*/
template <typename Mint, typename F>
std::vector<Mint> sample_point_shift(const std::vector<Mint>& ys, const Mint& m,
const F& multiply) {
Enumeration<Mint> comb;
int d = (int)ys.size() - 1;
std::vector<Mint> f(d + 1), g(d * 2 + 1);
for (int i = 0; i <= d; i++) {
f[i] = ys[i] * comb.finv(i) * comb.finv(d - i);
if ((d - i) & 1) f[i] = -f[i];
}
for (int i = 0; i <= 2 * d; i++) {
g[i] = Mint(1) / (m - d + i);
}
auto h = multiply(f, g);
Mint coef = 1;
for (int i = 0; i <= d; i++) {
coef *= (m - d + i);
}
for (int i = 0; i <= d; i++) {
h[i + d] *= coef;
coef *= (m + i + 1) * g[i];
}
return std::vector<Mint>{std::begin(h) + d, std::begin(h) + 2 * d + 1};
}
#line 9 "math/combinatorics/factorial.hpp"
/**
* @brief Factorial(階乗)
*/
template <typename Mint, typename F>
Mint factorial(std::int64_t n, const F& multiply) {
if (n <= 1) return 1;
if (n >= Mint::mod()) return 0;
std::int64_t v = 1;
while (v * v < n) v *= 2;
Mint iv = Mint(1) / v;
std::vector<Mint> G{1, v + 1};
for (std::int64_t d = 1; d != v; d <<= 1) {
std::vector<Mint> G1 = sample_point_shift(G, Mint(d) * iv, multiply);
std::vector<Mint> G2 =
sample_point_shift(G, Mint(d * v + v) * iv, multiply);
std::vector<Mint> G3 =
sample_point_shift(G, Mint(d * v + d + v) * iv, multiply);
for (int i = 0; i <= d; i++) G[i] *= G1[i], G2[i] *= G3[i];
std::copy(std::begin(G2), std::end(G2) - 1, std::back_inserter(G));
}
Mint res = 1;
std::int64_t i = 0;
while (i + v <= n) res *= G[i / v], i += v;
while (i < n) res *= ++i;
return res;
}
#line 2 "math/combinatorics/montgomery-mod-int.hpp"
#line 5 "math/combinatorics/montgomery-mod-int.hpp"
template <std::uint32_t mod_, bool fast = false>
struct MontgomeryModInt {
private:
using mint = MontgomeryModInt;
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
static constexpr u32 get_r() {
u32 ret = mod_;
for (i32 i = 0; i < 4; i++) ret *= 2 - mod_ * ret;
return ret;
}
static constexpr u32 r = get_r();
static constexpr u32 n2 = -u64(mod_) % mod_;
static_assert(r * mod_ == 1, "invalid, r * mod != 1");
static_assert(mod_ < (1 << 30), "invalid, mod >= 2 ^ 30");
static_assert((mod_ & 1) == 1, "invalid, mod % 2 == 0");
u32 x;
public:
MontgomeryModInt() : x{} {}
MontgomeryModInt(const i64& a)
: x(reduce(u64(fast ? a : (a % mod() + mod())) * n2)) {}
static constexpr u32 reduce(const u64& b) {
return u32(b >> 32) + mod() - u32((u64(u32(b) * r) * mod()) >> 32);
}
mint& operator+=(const mint& p) {
if (i32(x += p.x - 2 * mod()) < 0) x += 2 * mod();
return *this;
}
mint& operator-=(const mint& p) {
if (i32(x -= p.x) < 0) x += 2 * mod();
return *this;
}
mint& operator*=(const mint& p) {
x = reduce(u64(x) * p.x);
return *this;
}
mint& operator/=(const mint& p) {
*this *= p.inv();
return *this;
}
mint operator-() const { return mint() - *this; }
mint operator+(const mint& p) const { return mint(*this) += p; }
mint operator-(const mint& p) const { return mint(*this) -= p; }
mint operator*(const mint& p) const { return mint(*this) *= p; }
mint operator/(const mint& p) const { return mint(*this) /= p; }
bool operator==(const mint& p) const {
return (x >= mod() ? x - mod() : x) == (p.x >= mod() ? p.x - mod() : p.x);
}
bool operator!=(const mint& p) const {
return (x >= mod() ? x - mod() : x) != (p.x >= mod() ? p.x - mod() : p.x);
}
u32 val() const {
u32 ret = reduce(x);
return ret >= mod() ? ret - mod() : ret;
}
mint pow(u64 n) const {
mint ret(1), mul(*this);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
mint inv() const { return pow(mod() - 2); }
friend std::ostream& operator<<(std::ostream& os, const mint& p) {
return os << p.val();
}
friend std::istream& operator>>(std::istream& is, mint& a) {
i64 t;
is >> t;
a = mint(t);
return is;
}
static constexpr u32 mod() { return mod_; }
};
template <std::uint32_t mod>
using modint = MontgomeryModInt<mod>;
using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1000000007>;
#line 2 "math/fft/arbitrary-mod-convolution.hpp"
#line 5 "math/fft/arbitrary-mod-convolution.hpp"
#line 2 "math/fft/fast-fourier-transform.hpp"
#line 4 "math/fft/fast-fourier-transform.hpp"
#include <cassert>
#include <cmath>
#line 8 "math/fft/fast-fourier-transform.hpp"
namespace FastFourierTransform {
using real = double;
struct C {
real x, y;
C() : x(0), y(0) {}
C(real x, real y) : x(x), y(y) {}
inline C operator+(const C& c) const { return C(x + c.x, y + c.y); }
inline C operator-(const C& c) const { return C(x - c.x, y - c.y); }
inline C operator*(const C& c) const {
return C(x * c.x - y * c.y, x * c.y + y * c.x);
}
inline C conj() const { return C(x, -y); }
};
const real PI = std::acos(-1);
int base = 1;
std::vector<C> rts = {{0, 0}, {1, 0}};
std::vector<int> rev = {0, 1};
void ensure_base(int nbase) {
if (nbase <= base) return;
rev.resize(1 << nbase);
rts.resize(1 << nbase);
for (int i = 0; i < (1 << nbase); i++) {
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
}
while (base < nbase) {
real angle = PI * 2.0 / (1 << (base + 1));
for (int i = 1 << (base - 1); i < (1 << base); i++) {
rts[i << 1] = rts[i];
real angle_i = angle * (2 * i + 1 - (1 << base));
rts[(i << 1) + 1] = C(std::cos(angle_i), std::sin(angle_i));
}
++base;
}
}
void fft(std::vector<C>& a, int n) {
assert((n & (n - 1)) == 0);
int zeros = __builtin_ctz(n);
ensure_base(zeros);
int shift = base - zeros;
for (int i = 0; i < n; i++) {
if (i < (rev[i] >> shift)) {
std::swap(a[i], a[rev[i] >> shift]);
}
}
for (int k = 1; k < n; k <<= 1) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
C z = a[i + j + k] * rts[j + k];
a[i + j + k] = a[i + j] - z;
a[i + j] = a[i + j] + z;
}
}
}
}
std::vector<std::int64_t> multiply(const std::vector<int>& a,
const std::vector<int>& b) {
int need = (int)a.size() + (int)b.size() - 1;
int nbase = 1;
while ((1 << nbase) < need) nbase++;
ensure_base(nbase);
int sz = 1 << nbase;
std::vector<C> fa(sz);
for (int i = 0; i < sz; i++) {
int x = (i < (int)a.size() ? a[i] : 0);
int y = (i < (int)b.size() ? b[i] : 0);
fa[i] = C(x, y);
}
fft(fa, sz);
C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0);
for (int i = 0; i <= (sz >> 1); i++) {
int j = (sz - i) & (sz - 1);
C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r;
fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r;
fa[i] = z;
}
for (int i = 0; i < (sz >> 1); i++) {
C A0 = (fa[i] + fa[i + (sz >> 1)]) * t;
C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i];
fa[i] = A0 + A1 * s;
}
fft(fa, sz >> 1);
std::vector<std::int64_t> ret(need);
for (int i = 0; i < need; i++) {
ret[i] = std::llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x);
}
return ret;
}
}; // namespace FastFourierTransform
#line 7 "math/fft/arbitrary-mod-convolution.hpp"
/*
* @brief Arbitrary Mod Convolution(任意mod畳み込み)
*/
template <typename T>
struct ArbitraryModConvolution {
using real = FastFourierTransform::real;
using C = FastFourierTransform::C;
ArbitraryModConvolution() = default;
static std::vector<T> multiply(const std::vector<T>& a,
const std::vector<T>& b, int need = -1) {
if (need == -1) need = a.size() + b.size() - 1;
int nbase = 0;
while ((1 << nbase) < need) nbase++;
FastFourierTransform::ensure_base(nbase);
int sz = 1 << nbase;
std::vector<C> fa(sz);
for (int i = 0; i < a.size(); i++) {
fa[i] = C(a[i].val() & ((1 << 15) - 1), a[i].val() >> 15);
}
fft(fa, sz);
std::vector<C> fb(sz);
if (a == b) {
fb = fa;
} else {
for (int i = 0; i < b.size(); i++) {
fb[i] = C(b[i].val() & ((1 << 15) - 1), b[i].val() >> 15);
}
fft(fb, sz);
}
real ratio = 0.25 / sz;
C r2(0, -1), r3(ratio, 0), r4(0, -ratio), r5(0, 1);
for (int i = 0; i <= (sz >> 1); i++) {
int j = (sz - i) & (sz - 1);
C a1 = (fa[i] + fa[j].conj());
C a2 = (fa[i] - fa[j].conj()) * r2;
C b1 = (fb[i] + fb[j].conj()) * r3;
C b2 = (fb[i] - fb[j].conj()) * r4;
if (i != j) {
C c1 = (fa[j] + fa[i].conj());
C c2 = (fa[j] - fa[i].conj()) * r2;
C d1 = (fb[j] + fb[i].conj()) * r3;
C d2 = (fb[j] - fb[i].conj()) * r4;
fa[i] = c1 * d1 + c2 * d2 * r5;
fb[i] = c1 * d2 + c2 * d1;
}
fa[j] = a1 * b1 + a2 * b2 * r5;
fb[j] = a1 * b2 + a2 * b1;
}
fft(fa, sz);
fft(fb, sz);
std::vector<T> ret(need);
for (int i = 0; i < need; i++) {
std::int64_t aa = std::llround(fa[i].x);
std::int64_t bb = std::llround(fb[i].x);
std::int64_t cc = std::llround(fa[i].y);
aa = T(aa).val(), bb = T(bb).val(), cc = T(cc).val();
ret[i] = aa + (bb << 15) + (cc << 30);
}
return ret;
}
};
#line 9 "test/verify/yukicoder-502.test.cpp"
using namespace std;
using mint = modint1000000007;
int main() {
int N;
cin >> N;
ArbitraryModConvolution<mint> fft;
auto f = [&](vector<mint>& a, vector<mint>& b) { return fft.multiply(a, b); };
cout << factorial<mint>(N, f) << "\n";
}
| Env | Name | Status | Elapsed | Memory |
|---|---|---|---|---|
| g++ | 00_n0 |
|
2 ms | 4 MB |
| g++ | 00_n1 |
|
2 ms | 4 MB |
| g++ | 00_n10 |
|
2 ms | 4 MB |
| g++ | 00_n100 |
|
2 ms | 4 MB |
| g++ | 00_n11 |
|
2 ms | 4 MB |
| g++ | 00_n12 |
|
2 ms | 4 MB |
| g++ | 00_n13 |
|
2 ms | 4 MB |
| g++ | 00_n14 |
|
2 ms | 4 MB |
| g++ | 00_n15 |
|
2 ms | 4 MB |
| g++ | 00_n16 |
|
2 ms | 4 MB |
| g++ | 00_n17 |
|
2 ms | 4 MB |
| g++ | 00_n18 |
|
2 ms | 4 MB |
| g++ | 00_n19 |
|
2 ms | 4 MB |
| g++ | 00_n2 |
|
2 ms | 4 MB |
| g++ | 00_n20 |
|
2 ms | 4 MB |
| g++ | 00_n3 |
|
2 ms | 4 MB |
| g++ | 00_n4 |
|
2 ms | 4 MB |
| g++ | 00_n5 |
|
2 ms | 4 MB |
| g++ | 00_n6 |
|
2 ms | 4 MB |
| g++ | 00_n7 |
|
2 ms | 4 MB |
| g++ | 00_n8 |
|
2 ms | 4 MB |
| g++ | 00_n9 |
|
2 ms | 4 MB |
| g++ | 20_small1 |
|
4 ms | 4 MB |
| g++ | 20_small10 |
|
4 ms | 4 MB |
| g++ | 20_small2 |
|
3 ms | 4 MB |
| g++ | 20_small3 |
|
4 ms | 4 MB |
| g++ | 20_small4 |
|
3 ms | 4 MB |
| g++ | 20_small5 |
|
4 ms | 4 MB |
| g++ | 20_small6 |
|
3 ms | 4 MB |
| g++ | 20_small7 |
|
4 ms | 4 MB |
| g++ | 20_small8 |
|
3 ms | 4 MB |
| g++ | 20_small9 |
|
4 ms | 4 MB |
| g++ | 30_medium1 |
|
65 ms | 8 MB |
| g++ | 30_medium10 |
|
64 ms | 8 MB |
| g++ | 30_medium2 |
|
65 ms | 8 MB |
| g++ | 30_medium3 |
|
65 ms | 8 MB |
| g++ | 30_medium4 |
|
33 ms | 6 MB |
| g++ | 30_medium5 |
|
64 ms | 8 MB |
| g++ | 30_medium6 |
|
64 ms | 8 MB |
| g++ | 30_medium7 |
|
65 ms | 8 MB |
| g++ | 30_medium8 |
|
65 ms | 8 MB |
| g++ | 30_medium9 |
|
32 ms | 6 MB |
| g++ | 40_large1 |
|
2 ms | 4 MB |
| g++ | 40_large10 |
|
2 ms | 4 MB |
| g++ | 40_large2 |
|
2 ms | 4 MB |
| g++ | 40_large3 |
|
2 ms | 4 MB |
| g++ | 40_large4 |
|
2 ms | 4 MB |
| g++ | 40_large5 |
|
2 ms | 4 MB |
| g++ | 40_large6 |
|
2 ms | 4 MB |
| g++ | 40_large7 |
|
2 ms | 4 MB |
| g++ | 40_large8 |
|
2 ms | 4 MB |
| g++ | 40_large9 |
|
2 ms | 4 MB |
| clang++ | 00_n0 |
|
2 ms | 4 MB |
| clang++ | 00_n1 |
|
2 ms | 4 MB |
| clang++ | 00_n10 |
|
2 ms | 4 MB |
| clang++ | 00_n100 |
|
2 ms | 4 MB |
| clang++ | 00_n11 |
|
2 ms | 4 MB |
| clang++ | 00_n12 |
|
2 ms | 4 MB |
| clang++ | 00_n13 |
|
2 ms | 4 MB |
| clang++ | 00_n14 |
|
2 ms | 4 MB |
| clang++ | 00_n15 |
|
2 ms | 4 MB |
| clang++ | 00_n16 |
|
2 ms | 4 MB |
| clang++ | 00_n17 |
|
2 ms | 4 MB |
| clang++ | 00_n18 |
|
2 ms | 4 MB |
| clang++ | 00_n19 |
|
2 ms | 4 MB |
| clang++ | 00_n2 |
|
2 ms | 4 MB |
| clang++ | 00_n20 |
|
2 ms | 4 MB |
| clang++ | 00_n3 |
|
2 ms | 4 MB |
| clang++ | 00_n4 |
|
2 ms | 4 MB |
| clang++ | 00_n5 |
|
2 ms | 4 MB |
| clang++ | 00_n6 |
|
2 ms | 4 MB |
| clang++ | 00_n7 |
|
2 ms | 4 MB |
| clang++ | 00_n8 |
|
2 ms | 4 MB |
| clang++ | 00_n9 |
|
2 ms | 4 MB |
| clang++ | 20_small1 |
|
4 ms | 4 MB |
| clang++ | 20_small10 |
|
4 ms | 4 MB |
| clang++ | 20_small2 |
|
3 ms | 4 MB |
| clang++ | 20_small3 |
|
4 ms | 4 MB |
| clang++ | 20_small4 |
|
3 ms | 4 MB |
| clang++ | 20_small5 |
|
4 ms | 4 MB |
| clang++ | 20_small6 |
|
3 ms | 4 MB |
| clang++ | 20_small7 |
|
4 ms | 4 MB |
| clang++ | 20_small8 |
|
3 ms | 4 MB |
| clang++ | 20_small9 |
|
4 ms | 4 MB |
| clang++ | 30_medium1 |
|
71 ms | 8 MB |
| clang++ | 30_medium10 |
|
71 ms | 8 MB |
| clang++ | 30_medium2 |
|
71 ms | 8 MB |
| clang++ | 30_medium3 |
|
71 ms | 8 MB |
| clang++ | 30_medium4 |
|
36 ms | 6 MB |
| clang++ | 30_medium5 |
|
71 ms | 8 MB |
| clang++ | 30_medium6 |
|
71 ms | 8 MB |
| clang++ | 30_medium7 |
|
71 ms | 8 MB |
| clang++ | 30_medium8 |
|
71 ms | 8 MB |
| clang++ | 30_medium9 |
|
36 ms | 6 MB |
| clang++ | 40_large1 |
|
2 ms | 4 MB |
| clang++ | 40_large10 |
|
2 ms | 4 MB |
| clang++ | 40_large2 |
|
2 ms | 4 MB |
| clang++ | 40_large3 |
|
2 ms | 4 MB |
| clang++ | 40_large4 |
|
2 ms | 4 MB |
| clang++ | 40_large5 |
|
2 ms | 4 MB |
| clang++ | 40_large6 |
|
2 ms | 4 MB |
| clang++ | 40_large7 |
|
2 ms | 4 MB |
| clang++ | 40_large8 |
|
2 ms | 4 MB |
| clang++ | 40_large9 |
|
2 ms | 4 MB |