結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー leafirbyleafirby
提出日時 2020-05-01 17:37:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,174 bytes
コンパイル時間 905 ms
コンパイル使用メモリ 79,212 KB
実行使用メモリ 13,656 KB
最終ジャッジ日時 2024-11-06 01:57:57
合計ジャッジ時間 9,750 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//squareさんのコード(epsf001-ex)

#ifndef CLASS_FAST_MODINT
#define CLASS_FAST_MODINT
 
#include <cstdint>
 
using singlebit = uint32_t;
using doublebit = uint64_t;
static constexpr int digit_level = 8 * sizeof(singlebit);
 
static constexpr singlebit find_inv(singlebit n, int d = 6, singlebit x = 1) {
    return d == 0 ? x : find_inv(n, d - 1, x * (2 - x * n));
}
template <singlebit mod> class fast_modint {
    // Fast Modulo Integer, Assertion: mod < 2^(bits of singlebit - 1) and mod is prime
private:
    singlebit n;
    static constexpr singlebit r2 = (((doublebit(1) << digit_level) % mod) << digit_level) % mod;
    static constexpr singlebit ninv = singlebit(-1) * find_inv(mod);
    singlebit reduce(doublebit x) const {
        singlebit res = (x + doublebit(singlebit(x) * ninv) * mod) >> digit_level;
        return res < mod ? res : res - mod;
    }
public:
    fast_modint() : n(0) {};
    fast_modint(singlebit n_) { n = reduce(doublebit(n_) * r2); };
    static constexpr singlebit get_mod() { return mod; }
    singlebit get() const { return reduce(n); }
    bool operator==(const fast_modint& x) const { return n == x.n; }
    bool operator!=(const fast_modint& x) const { return n != x.n; }
    fast_modint& operator+=(const fast_modint& x) { n += x.n; n -= (n < mod ? 0 : mod); return *this; }
    fast_modint& operator-=(const fast_modint& x) { n += mod - x.n; n -= (n < mod ? 0 : mod); return *this; }
    fast_modint& operator*=(const fast_modint& x) { n = reduce(doublebit(n) * x.n); return *this; }
    fast_modint operator+(const fast_modint& x) const { return fast_modint(*this) += x; }
    fast_modint operator-(const fast_modint& x) const { return fast_modint(*this) -= x; }
    fast_modint operator*(const fast_modint& x) const { return fast_modint(*this) *= x; }
    fast_modint inv() const { return binpow(mod - 2); }
    fast_modint binpow(singlebit b) const {
        fast_modint ans(1), cur(*this);
        while (b > 0) {
            if (b & 1) ans *= cur;
            cur *= cur;
            b >>= 1;
        }
        return ans;
    }
};
 
#endif // CLASS_FAST_MODINT
 
#ifndef CLASS_POLYNOMIAL_NTT
#define CLASS_POLYNOMIAL_NTT
 
#include <vector>
#include <algorithm>
 
template<singlebit mod, singlebit depth, singlebit primroot>
class polynomial_ntt {
public:
    using modulo = fast_modint<mod>;
    static void fourier_transform(std::vector<modulo>& v, bool inverse) {
        std::size_t s = v.size();
        for (std::size_t i = 0, j = 1; j < s - 1; ++j) {
            for (std::size_t k = s >> 1; k > (i ^= k); k >>= 1);
            if (i < j) std::swap(v[i], v[j]);
        }
        std::size_t sc = 0, sz = 1;
        while (sz < s) sz *= 2, ++sc;
        modulo root = modulo(primroot).binpow((mod - 1) >> sc);
        std::vector<modulo> pw(s + 1); pw[0] = 1;
        for (std::size_t i = 1; i <= s; i++) pw[i] = pw[i - 1] * root;
        std::size_t qs = s;
        for (std::size_t b = 1; b < s; b <<= 1) {
            qs >>= 1;
            for (std::size_t i = 0; i < s; i += b * 2) {
                for (std::size_t j = i; j < i + b; ++j) {
                    modulo delta = pw[(inverse ? b * 2 - j + i : j - i) * qs] * v[j + b];
                    v[j + b] = v[j] - delta;
                    v[j] += delta;
                }
            }
        }
        if (!inverse) return;
        modulo powinv = modulo((mod + 1) / 2).binpow(sc);
        for (std::size_t i = 0; i < s; ++i) {
            v[i] = v[i] * powinv;
        }
    }
    static std::vector<modulo> convolve(std::vector<modulo> v1, std::vector<modulo> v2) {
        std::size_t s1 = v1.size(), s2 = v2.size(), s = 1;
        while (s < s1 || s < s2) s *= 2;
        v1.resize(s * 2); fourier_transform(v1, false);
        v2.resize(s * 2); fourier_transform(v2, false);
        for (singlebit i = 0; i < s * 2; ++i) v1[i] *= v2[i];
        fourier_transform(v1, true);
        v1.resize(s1 + s2 - 1);
        return v1;
    }
};
 
#endif // CLASS_POLYNOMIAL_NTT
 
#include <vector>
#include <iostream>
using namespace std;
using ntt = polynomial_ntt<998244353, 23, 3>;
using mint = ntt::modulo;
mint factorial(int N) {
    // 1, 1, 2, 6, 24, 120, 720,...
    mint ans = 1;
    for (int i = 1; i <= N; ++i) {
        ans *= i;
    }
    return ans;
}
mint derangement(int N) {
    // 1, 0, 1, 2, 9, 44, 265,...
    mint ans = 1;
    for (int i = 1; i <= N; ++i) {
        ans *= i;
        if (i % 2 == 1) ans -= 1;
        else ans += 1;
    }
    return ans;
}
vector<mint> pow(vector<mint> poly, int b) {
    vector<mint> ans({ mint(1) });
    while (b) {
        if (b & 1) ans = ntt::convolve(ans, poly);
        poly = ntt::convolve(poly, poly);
        b >>= 1;
    }
    return ans;
}
int main() {
    int N;
    cin >> N;
    vector<mint> poly = pow({ mint(1), mint(4), mint(2) }, N / 2);
    if (N % 2 == 1) poly = ntt::convolve(poly, { mint(1), mint(1) });
    mint ans = 0, mul = 1;
    for (int i = 0; i <= N; ++i) {
        if ((N - i) % 2 == 0) ans += poly[N - i] * mul;
        else ans -= poly[N - i] * mul;
        mul *= i + 1;
    }
    cout << (ans + factorial(N) - derangement(N) * 2).get() << endl;
    return 0;
}
0