結果

問題 No.2130 分配方法の数え上げ mod 998244353
ユーザー みここみここ
提出日時 2022-12-11 03:11:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 4,566 bytes
コンパイル時間 726 ms
コンパイル使用メモリ 78,176 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 03:14:31
合計ジャッジ時間 1,990 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
typedef long long ll;

const int MOD = 998244353;

struct mint
{
    int val;

    mint() : val(0) {}

    mint(long long v)
    {
        if (abs(v) >= MOD)
        {
            v %= MOD;
        }
        if (v < 0)
        {
            v += MOD;
        }
        val = v;
    }

    mint &operator++()
    {
        val++;
        if (val == MOD)
        {
            val = 0;
        }
        return *this;
    }

    mint &operator--()
    {
        if (val == 0)
        {
            val = MOD;
        }
        val--;
        return *this;
    }

    mint &operator+=(const mint &x)
    {
        val += x.val;
        if (val >= MOD)
        {
            val -= MOD;
        }
        return *this;
    }

    mint &operator-=(const mint &x)
    {
        val -= x.val;
        if (val < 0)
        {
            val += MOD;
        }
        return *this;
    }

    mint &operator*=(const mint &x)
    {
        val = (int)((long long)val * x.val % MOD);
        return *this;
    }

    mint &operator/=(const mint &x)
    {
        *this *= x.inv();
        return *this;
    }

    mint operator-()
    {
        return mint() - *this;
    }

    mint pow(long long n) const
    {
        mint x = 1, r = *this;
        while (n)
        {
            if (n & 1)
            {
                x *= r;
            }
            r *= r;
            n >>= 1;
        }
        return x;
    }

    mint inv() const
    {
        return pow(MOD - 2);
    }

    friend mint operator+(const mint &x, const mint &y)
    {
        return mint(x) += y;
    }

    friend mint operator-(const mint &x, const mint &y)
    {
        return mint(x) -= y;
    }

    friend mint operator*(const mint &x, const mint &y)
    {
        return mint(x) *= y;
    }

    friend mint operator/(const mint &x, const mint &y)
    {
        return mint(x) /= y;
    }

    friend bool operator==(const mint &x, const mint &y)
    {
        return x.val == y.val;
    }

    friend bool operator!=(const mint &x, const mint &y)
    {
        return x.val != y.val;
    }

    friend std::ostream &operator<<(std::ostream &os, const mint &x)
    {
        return os << x.val;
    }

    friend std::istream &operator>>(std::istream &is, mint &x)
    {
        int v;
        is >> v;
        x = mint(v);
        return is;
    }
};

mint modpow(mint x, long long n)
{
    mint res = 1, r = x;
    while (n)
    {
        if (n & 1)
        {
            res *= r;
        }
        r *= r;
        n >>= 1;
    }
    return res;
}

struct combination_big
{
    long long maxn;
    int maxk;
    std::vector<mint> nP;
    std::vector<mint> inP;
    std::vector<mint> fact;
    std::vector<mint> ifact;

    combination_big(long long maxn, int maxk) : maxn(maxn), maxk(maxk), nP(maxk + 1), inP(maxk + 1), fact(maxk + 1), ifact(maxk + 1)
    {
        nP[0] = 1;
        inP[0] = 1;
        fact[0] = 1;
        ifact[0] = 1;
        for (int i = 1; i <= maxk; i++)
        {
            nP[i] = nP[i - 1] * (maxn + 1 - i);
            inP[i] = inP[i - 1] * modpow(maxn + 1 - i, MOD - 2);
            fact[i] = fact[i - 1] * i;
            ifact[i] = ifact[i - 1] * modpow(i, MOD - 2);
        }
    }

    mint operator()(long long n, int k)
    {
        assert(n <= maxn);
        assert(maxn - n + k <= maxk);
        if (k > n || k < 0)
        {
            return 0;
        }
        return nP[maxn - n + k] * inP[maxn - n] * ifact[k];
    }
};

struct combination_fix_n
{
    long long n;
    int maxk;
    std::vector<mint> nP;
    std::vector<mint> fact;
    std::vector<mint> ifact;

    combination_fix_n(long long n, int maxk) : n(n), maxk(maxk)
    {
        nP.resize(maxk + 1);
        fact.resize(maxk + 1);
        ifact.resize(maxk + 1);
        nP[0] = 1;
        fact[0] = 1;
        for (int i = 1; i <= maxk; i++)
        {
            nP[i] = nP[i - 1] * (n + 1 - i);
            fact[i] = fact[i - 1] * i;
        }
        ifact[maxk] = modpow(fact[maxk], MOD - 2);
        for (int i = maxk - 1; i >= 0; i--)
        {
            ifact[i] = ifact[i + 1] * (i + 1);
        }
    }

    mint operator()(int k)
    {
        assert(k <= maxk);
        if (k > n || k < 0)
        {
            return 0;
        }
        return nP[k] * ifact[k];
    }
};

int main()
{
    ll n, m;
    cin >> n >> m;
    combination_big com(n + 3, m + 3);
    mint ans = modpow(2, n);
    for (int i = 0; i < m; i++)
    {
        ans -= com(n, i);
    }
    cout << ans << endl;
}
0