結果

問題 No.2668 Trees on Graph Paper
ユーザー みここみここ
提出日時 2024-03-08 22:57:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 829 ms / 3,000 ms
コード長 3,296 bytes
コンパイル時間 568 ms
コンパイル使用メモリ 70,460 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 22:57:25
合計ジャッジ時間 10,109 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 70 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 469 ms
6,676 KB
testcase_15 AC 738 ms
6,676 KB
testcase_16 AC 360 ms
6,676 KB
testcase_17 AC 37 ms
6,676 KB
testcase_18 AC 702 ms
6,676 KB
testcase_19 AC 584 ms
6,676 KB
testcase_20 AC 678 ms
6,676 KB
testcase_21 AC 605 ms
6,676 KB
testcase_22 AC 618 ms
6,676 KB
testcase_23 AC 391 ms
6,676 KB
testcase_24 AC 806 ms
6,676 KB
testcase_25 AC 782 ms
6,676 KB
testcase_26 AC 812 ms
6,676 KB
testcase_27 AC 829 ms
6,676 KB
testcase_28 AC 1 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

struct DynamicModint
{
    using mint = DynamicModint;

public:
    int val;

    DynamicModint() : val(0) {}

    DynamicModint(long long v)
    {
        if (std::abs(v) >= mod())
        {
            v %= mod();
        }
        if (v < 0)
        {
            v += mod();
        }
        val = v;
    }

    static void set_mod(int m)
    {
        MOD = m;
    }

    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)
    {
        long long v;
        is >> v;
        x = mint(v);
        return is;
    }

private:
    static int MOD;
    static int mod()
    {
        return MOD;
    }
};

int DynamicModint::MOD = 998244353;

using mint = DynamicModint;

int main()
{
    int n, m;
    cin >> n >> m;
    DynamicModint::set_mod(m);
    mint dp[8];
    dp[4] = 1;
    mint ans = 1;
    for (int j = 2; j <= n * 2 - 1; j++)
    {
        mint d[8];
        d[1] = dp[1] + dp[2] + dp[3];
        d[2] = dp[1] * (j - 2) + dp[4] + dp[5];
        d[3] = dp[2] + dp[3] + dp[6] + dp[7];
        d[4] = dp[1] * (j - 2) * (j - 1);
        d[5] = dp[2] * (j - 1) + dp[3] * (j - 1);
        d[6] = dp[4] + dp[5];
        d[7] = dp[6] + dp[7];
        swap(dp, d);
        if (j % 2)
        {
            ans *= dp[1];
        }
    }
    for (int i = 1; i <= n * 2 - 1; i++)
    {
        ans *= i;
    }
    cout << ans << endl;
}
0