結果

問題 No.2668 Trees on Graph Paper
ユーザー k1suxuk1suxu
提出日時 2024-03-09 00:33:10
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 773 ms / 3,000 ms
コード長 7,437 bytes
コンパイル時間 3,036 ms
コンパイル使用メモリ 251,456 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-09 00:33:24
合計ジャッジ時間 12,540 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 67 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 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 2 ms
6,676 KB
testcase_14 AC 476 ms
6,676 KB
testcase_15 AC 684 ms
6,676 KB
testcase_16 AC 383 ms
6,676 KB
testcase_17 AC 35 ms
6,676 KB
testcase_18 AC 647 ms
6,676 KB
testcase_19 AC 570 ms
6,676 KB
testcase_20 AC 632 ms
6,676 KB
testcase_21 AC 577 ms
6,676 KB
testcase_22 AC 618 ms
6,676 KB
testcase_23 AC 375 ms
6,676 KB
testcase_24 AC 773 ms
6,676 KB
testcase_25 AC 747 ms
6,676 KB
testcase_26 AC 760 ms
6,676 KB
testcase_27 AC 771 ms
6,676 KB
testcase_28 AC 2 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 #

// #pragma GCC target("avx")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>

template<typename T>
bool chmax(T &a, const T b) {if(a<b) {a=b; return true;} else {return false;}}
template<typename T>
bool chmin(T &a, const T b) {if(a>b) {a=b; return true;} else {return false;}}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

#define int long long

struct Arbitrary_Modular_Int {
    long long x;

    Arbitrary_Modular_Int() = default;
    Arbitrary_Modular_Int(long long x_) : x(x_ >= 0 ? x_ % get_mod() : (get_mod() - (-x_) % get_mod()) % get_mod()) {}
    
    // static long long& get_mod() {
    //     static long long mod = 0;
    //     return mod;
    // }
    static long long& get_mod() {
        static long long mod = 0;
        return mod;
    }
    static void set_mod(long long mod_) {
        get_mod() = mod_;
    }

    long long val() const {
        return (x%get_mod()+get_mod())%get_mod();
    }
    

    Arbitrary_Modular_Int& operator^=(long long d)  {
        Arbitrary_Modular_Int ret(1);
        long long nx = x;
        while(d) {
            if(d&1) ret *= nx;
            (nx *= nx) %= get_mod();
            d >>= 1;
        }
        *this = ret;
        return *this;
    }
    Arbitrary_Modular_Int operator^(long long d) const {return Arbitrary_Modular_Int(*this) ^= d;}
    Arbitrary_Modular_Int pow(long long d) const {return Arbitrary_Modular_Int(*this) ^= d;}
    
    //use this basically
    // Arbitrary_Modular_Int inv() const {
    //     return Arbitrary_Modular_Int(*this) ^ (get_mod()-2);
    // }
    //only if the module number is not prime
    //Don't use. This is broken.
    Arbitrary_Modular_Int inv() const {
        long long a = (x%get_mod()+get_mod())%get_mod(), b = get_mod(), u = 1, v = 0;
        while(b > 0) {
            long long t = a/b;
            a -= t*b, swap(a, b);
            u -= t*v, swap(u, v);
        }
        return Arbitrary_Modular_Int(u);
    }

    Arbitrary_Modular_Int& operator+=(const Arbitrary_Modular_Int other) {
        if((x += other.x) >= get_mod()) x -= get_mod();
        return *this;
    }
    Arbitrary_Modular_Int& operator-=(const Arbitrary_Modular_Int other) {
        if((x -= other.x) < 0) x += get_mod();
        return *this;
    }
    Arbitrary_Modular_Int& operator*=(const Arbitrary_Modular_Int other) {
        long long z = x;
        z *= other.x;
        z %= get_mod();
        x = z;
        if(x < 0) x += get_mod();
        return *this;
    }
    Arbitrary_Modular_Int& operator/=(const Arbitrary_Modular_Int other) {
        return *this = *this * other.inv();
    }
    Arbitrary_Modular_Int& operator++() {
        x++;
        if (x == get_mod()) x = 0;
        return *this;
    }
    Arbitrary_Modular_Int& operator--() {
        if (x == 0) x = get_mod();
        x--;
        return *this;
    }
    
    Arbitrary_Modular_Int operator+(const Arbitrary_Modular_Int other) const {return Arbitrary_Modular_Int(*this) += other;}
    Arbitrary_Modular_Int operator-(const Arbitrary_Modular_Int other) const {return Arbitrary_Modular_Int(*this) -= other;}
    Arbitrary_Modular_Int operator*(const Arbitrary_Modular_Int other) const {return Arbitrary_Modular_Int(*this) *= other;}
    Arbitrary_Modular_Int operator/(const Arbitrary_Modular_Int other) const {return Arbitrary_Modular_Int(*this) /= other;}
    
    Arbitrary_Modular_Int& operator+=(const long long other) {Arbitrary_Modular_Int other_(other); *this += other_; return *this;}
    Arbitrary_Modular_Int& operator-=(const long long other) {Arbitrary_Modular_Int other_(other); *this -= other_; return *this;}
    Arbitrary_Modular_Int& operator*=(const long long other) {Arbitrary_Modular_Int other_(other); *this *= other_; return *this;}
    Arbitrary_Modular_Int& operator/=(const long long other) {Arbitrary_Modular_Int other_(other); *this /= other_; return *this;}
    Arbitrary_Modular_Int operator+(const long long other) const {return Arbitrary_Modular_Int(*this) += other;}
    Arbitrary_Modular_Int operator-(const long long other) const {return Arbitrary_Modular_Int(*this) -= other;}
    Arbitrary_Modular_Int operator*(const long long other) const {return Arbitrary_Modular_Int(*this) *= other;}
    Arbitrary_Modular_Int operator/(const long long other) const {return Arbitrary_Modular_Int(*this) /= other;}

    bool operator==(const Arbitrary_Modular_Int other) const {return (*this).val() == other.val();}
    bool operator!=(const Arbitrary_Modular_Int other) const {return (*this).val() != other.val();}
    bool operator==(const long long other) const {return (*this).val() == other;}
    bool operator!=(const long long other) const {return (*this).val() != other;}

    Arbitrary_Modular_Int operator-() const {return Arbitrary_Modular_Int(0LL)-Arbitrary_Modular_Int(*this);}

    //-1: sqrtが存在しない
    //複数存在する場合どれを返すかは不明
    long long get_sqrt() {
        long long a = val(), p = get_mod();
        if(a == 0) return 0;
        if(p == 2) return a;
        if(Arbitrary_Modular_Int(a).pow((p - 1) >> 1).val() != 1) return -1;
        long long b = 1;
        while(Arbitrary_Modular_Int(b).pow((p - 1) >> 1).val() == 1) ++b;
        long long e = 0, m = p - 1;
        while(m % 2 == 0) m >>= 1, ++e;
        long long x = Arbitrary_Modular_Int(a).pow((m - 1) >> 1).val();
        long long y = a * (x * x % p) % p;
        (x *= a) %= p;
        long long z = Arbitrary_Modular_Int(b).pow(m).val();
        while(y != 1) {
            long long j = 0, t = y;
            while(t != 1) {
                j += 1;
                (t *= t) %= p;
            }
            z = Arbitrary_Modular_Int(z).pow((long long)1 << (e - j - 1)).val();
            (x *= z) %= p;
            (z *= z) %= p;
            (y *= z) %= p;
            e = j;
        }
        return x;
    }
};

istream& operator>>(istream& is, Arbitrary_Modular_Int& x) {
    long long X;
    is >> X;
    x = X;
    return is;
}
ostream& operator<<(ostream& os, Arbitrary_Modular_Int& x) {
    os << x.val();
    return os;
}

using mint = Arbitrary_Modular_Int;

void solve() {
    const int MX = 1e7;
    int n, m;
    cin >> n >> m;
    mint::set_mod(m);
    mint ans = 1;
    repi(i, 1, 2*n) ans *= i;
    vector<vector<mint>> dp(2, vector<mint>(3, 0));
    dp[1][0] = 1, dp[1][1] = 1, dp[1][2] = 1;
    repi(i, 1, 2*n-2) {
        int cur = i&1;
        int nxt = 1^cur;
        dp[nxt][0] = dp[cur][0] + dp[cur][1]*(i+1);
        dp[nxt][1] = dp[cur][0] + dp[cur][1] + dp[cur][2]*(i+1);
        dp[nxt][2] = dp[cur][0] + dp[cur][1] + dp[cur][2];
        if(i%2==0) ans *= dp[nxt][2];
    }
    cout << ans.val() << endl;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0