結果
問題 | No.3145 Astral Parentheses Sequence |
ユーザー |
|
提出日時 | 2025-05-16 22:42:28 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 14 ms / 2,000 ms |
コード長 | 2,802 bytes |
コンパイル時間 | 2,050 ms |
コンパイル使用メモリ | 201,748 KB |
実行使用メモリ | 6,272 KB |
最終ジャッジ日時 | 2025-05-16 22:42:32 |
合計ジャッジ時間 | 3,774 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 |
ソースコード
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; //入力が必ず-mod<a<modの時. struct mint{ long long v = 0; mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;} mint(unsigned long long a){v = a;} long long val(){return v;} void modu(){v %= mod;} mint &operator=(const mint &b) = default; mint operator-()const{return mint(0)-(*this);} mint operator+(const mint b)const{return mint(v)+=b;} mint operator-(const mint b)const{return mint(v)-=b;} mint operator*(const mint b)const{return mint(v)*=b;} mint operator/(const mint b)const{return mint(v)/=b;} mint operator+=(const mint b){ v += b.v; if(v >= mod) v -= mod; return *this; } mint operator-=(const mint b){ v -= b.v; if(v < 0) v += mod; return *this; } mint operator*=(const mint b){v = v*b.v%mod; return *this;} mint operator/=(mint b){ if(b == 0) assert(false); int left = mod-2; while(left){if(left&1) *this *= b; b *= b; left >>= 1;} return *this; } mint operator++(){*this += 1; return *this;} mint operator--(){*this -= 1; return *this;} mint operator++(int){*this += 1; return *this;} mint operator--(int){*this -= 1; return *this;} bool operator==(const mint b)const{return v == b.v;} bool operator!=(const mint b)const{return v != b.v;} bool operator>(const mint b)const{return v > b.v;} bool operator>=(const mint b)const{return v >= b.v;} bool operator<(const mint b)const{return v < b.v;} bool operator<=(const mint b)const{return v <= b.v;} mint pow(long long n)const{ mint ret = 1,p = v; if(n < 0) p = p.inv(),n = -n; while(n){ if(n&1) ret *= p; p *= p; n >>= 1; } return ret; } mint inv()const{return mint(1)/v;} }; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N >> mod; if(N%3){cout << 0 << endl; return 0;} N /= 3; vector<mint> dp(N+1); dp.at(0) = 1; vector<vector<mint>> dp2(N+1,vector<mint>(4)); dp2.at(0).at(0) = 1; for(int i=1; i<=N; i++){ for(int k=0; k<=3; k++) dp.at(i) += dp2.at(i-1).at(k); vector<mint> mul = {1,dp.at(i),dp.at(i)*dp.at(i),dp.at(i)*dp.at(i)*dp.at(i)}; for(int k=N-i; k>=0; k--) for(int l=1; l<=3; l++){ if(i*l+k > N) break; int to = i*l+k; for(int m=0; m<=3-l; m++){ mint d = dp2.at(k).at(m)*mul.at(l); if(m+l == 3) dp2.at(to).at(3) += d; else if(m == 1 && l == 1) dp2.at(to).at(2) += d*2; else dp2.at(to).at(m+l) += d*3; } } } cout << dp.at(N).v << endl; }