結果

問題 No.1923 Divisor Array
ユーザー ebi_flyebi_fly
提出日時 2022-05-01 23:49:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 4,292 bytes
コンパイル時間 966 ms
コンパイル使用メモリ 78,236 KB
最終ジャッジ日時 2025-01-29 01:26:42
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>

/*
計算量: O(M^2 + M\log{M}\log{K})
*/

/* macro */

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)

/* macro end */

namespace ebi {

template <std::uint_fast64_t Modulus>
class modint {
    using u64 = std::uint_fast64_t;

   public:
    u64 a;

    constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
    constexpr u64 &value() noexcept { return a; }
    constexpr u64 &val() noexcept { return a; }
    constexpr const u64 &value() const noexcept { return a; }
    constexpr modint operator+(const modint rhs) const noexcept {
        return modint(*this) += rhs;
    }
    constexpr modint operator-(const modint rhs) const noexcept {
        return modint(*this) -= rhs;
    }
    constexpr modint operator*(const modint rhs) const noexcept {
        return modint(*this) *= rhs;
    }
    constexpr modint operator/(const modint rhs) const noexcept {
        return modint(*this) /= rhs;
    }
    constexpr modint &operator+=(const modint rhs) noexcept {
        a += rhs.a;
        if (a >= Modulus) {
            a -= Modulus;
        }
        return *this;
    }
    constexpr modint &operator-=(const modint rhs) noexcept {
        if (a < rhs.a) {
            a += Modulus;
        }
        a -= rhs.a;
        return *this;
    }
    constexpr modint &operator*=(const modint rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
    }
    constexpr modint &operator/=(modint rhs) noexcept {
        u64 exp = Modulus - 2;
        while (exp) {
            if (exp % 2) {
                *this *= rhs;
            }
            rhs *= rhs;
            exp /= 2;
        }
        return *this;
    }
    constexpr modint operator-() const { return modint() - *this; }
    bool operator==(const u64 rhs) { return a == rhs; }
    bool operator!=(const u64 rhs) { return a != rhs; }
    constexpr modint &operator++() {
        a++;
        if (a == mod()) a = 0;
        return *this;
    }
    constexpr modint &operator--() {
        if (a == 0) a = mod();
        a--;
        return *this;
    }

    modint pow(u64 n) const noexcept {
        modint res = 1;
        modint x = a;
        while (n > 0) {
            if (n & 1) res *= x;
            x *= x;
            n >>= 1;
        }
        return res;
    }
    modint inv() const { return pow(Modulus - 2); }

    static u64 mod() { return Modulus; }
};

using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1000000007>;

template <std::uint_fast64_t Modulus>
std::ostream &operator<<(std::ostream &os, modint<Modulus> a) {
    return os << a.val();
}

}  // namespace ebi

using mint = ebi::modint998244353;

int main() {
    int n, m, k;
    std::cin >> n >> m >> k;
    std::vector dp(m + 1, std::vector<mint>(m, 0));
    dp[0][0] = 1;
    rep(i, 0, m) {
        rep(j, 0, m) {
            if (j + 1 < m) dp[i + 1][j + 1] += dp[i][j] + dp[i + 1][j];
            if (2 * j + 2 < m) dp[i + 1][2 * j + 2] -= dp[i][j];
        }
    }
    std::vector<mint> ret(m + 1, 0);
    std::vector<mint> fact(m + 1, 1), inv(m + 1, 1);
    rep(i, 0, m) {
        fact[i + 1] = fact[i] * mint(n - i);
        inv[i + 1] = inv[i] * mint(i + 1);
    }
    inv[m] = inv[m].inv();
    rrep(i, 0, m) { inv[i] = inv[i + 1] * mint(i + 1); }
    auto comb = [&](int r) -> mint {
        assert(0 <= r && r <= m);
        return fact[r] * inv[r];
    };
    rep(i, 0, std::min(n, m) + 1) {
        rep(j, 0, m) { ret[j + 1] += dp[i][j] * comb(i); }
    }
    std::vector<mint> now(m + 1, 0);
    now[1] = 1;
    auto mul = [&m](const std::vector<mint> &lhs,
                    const std::vector<mint> &rhs) -> std::vector<mint> {
        std::vector<mint> res(m + 1, 0);
        for (int i = 1; i < m + 1; i++) {
            for (int j = 1; i * j < m + 1; j++) {
                res[i * j] += lhs[i] * rhs[j];
            }
        }
        return res;
    };
    while (k > 0) {
        if (k & 1) {
            now = mul(now, ret);
        }
        ret = mul(ret, ret);
        k >>= 1;
    }
    mint ans = 0;
    for (int i = 0; i < m + 1; i++) {
        ans += now[i];
    }
    std::cout << ans.val() << '\n';
}
0