結果
| 問題 |
No.3145 Astral Parentheses Sequence
|
| コンテスト | |
| ユーザー |
ococonomy1
|
| 提出日時 | 2025-05-16 22:41:58 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,040 ms / 2,000 ms |
| コード長 | 6,653 bytes |
| コンパイル時間 | 2,328 ms |
| コンパイル使用メモリ | 198,932 KB |
| 実行使用メモリ | 6,272 KB |
| 最終ジャッジ日時 | 2025-05-16 22:42:18 |
| 合計ジャッジ時間 | 18,078 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 45 |
ソースコード
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pli = pair<ll,int>;
#define AMARI 998244353
//#define AMARI 1000000007
#define el '\n'
#define El '\n'
#define YESNO(x) ((x) ? "Yes" : "No")
#define YES YESNO(true)
#define NO YESNO(false)
#define REV_PRIORITY_QUEUE(tp) priority_queue<tp,vector<tp>,greater<tp>>
#define EXIT_ANS(x) {cout << (x) << '\n'; return;}
template <typename T> void inline SORT(vector<T> &v){sort(v.begin(),v.end()); return;}
template <typename T> void inline VEC_UNIQ(vector<T> &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;}
template <typename T> T inline MAX(vector<T> &v){return *max_element(v.begin(),v.end());}
template <typename T> T inline MIN(vector<T> &v){return *min_element(v.begin(),v.end());}
template <typename T> T inline SUM(vector<T> &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;}
void inline TEST(void){cerr << "TEST" << endl; return;}
template <typename T> bool inline chmin(T &x,T y){
if(x > y){
x = y;
return true;
}
return false;
}
template <typename T> bool inline chmax(T &x,T y){
if(x < y){
x = y;
return true;
}
return false;
}
template <typename T = long long> vector<T> inline get_vec(int n){
vector<T> ans(n);
for(int i = 0; i < n; i++)cin >> ans[i];
return ans;
}
template <typename T> void inline print_vec(vector<T> &vec,bool kaigyou = false){
int n = (int)vec.size();
for(int i = 0; i < n; i++){
cout << vec[i];
if(kaigyou || i == n - 1)cout << '\n';
else cout << ' ';
}
if(!n)cout << '\n';
return;
}
template <typename T> void inline debug_vec(vector<T> &vec,bool kaigyou = false){
int n = (int)vec.size();
for(int i = 0; i < n; i++){
cerr << vec[i];
if(kaigyou || i == n - 1)cerr << '\n';
else cerr << ' ';
}
if(!n)cerr << '\n';
return;
}
vector<vector<int>> inline get_graph(int n,int m = -1,bool direct = false){
if(m == -1)m = n - 1;
vector<vector<int>> g(n);
while(m--){
int u,v;
cin >> u >> v;
u--; v--;
g[u].push_back(v);
if(!direct)g[v].push_back(u);
}
return g;
}
//自作 modint
//雑に色んな operator でディープコピーしまくる実装にしたらベタ書きに比べて相当遅くなったため、 ACL をかなり参考にしている。
//割り算について、 mod が素数かどうかを確かめずにフェルマーの小定理を使うあれをやっているので、 mod が素数でない時はかなり注意。
template<int m> class ococo_static_modint{
private:
using mint = ococo_static_modint;
mint pow(long long b)const{
assert(b >= 0);
mint ans = 1,temp = *this;
while(b){
if(b & 1)ans *= temp;
temp *= temp;
b >>= 1;
}
return ans;
}
mint inv(void)const{
return pow(m - 2);
}
public:
//これを書くことで初期値を決めてくれるらしい
ococo_static_modint() : val(0){}
//long long への変換
//operator long long(){return val;}
template <typename T>
ococo_static_modint(T x){
long long xv = (long long)(x) % m;
if(xv < 0)xv += m;
val = xv;
}
long long val = 0;
void operator++(int){
val++;
if(val == m)val = 0;
return;
}
void operator--(int){
if(val == 0)val = m;
val--;
return;
}
mint& operator+=(const mint& r){
val += r.val;
if(val >= m)val -= m;
return *this;
}
mint& operator-=(const mint& r){
val -= r.val;
if(val < 0)val += m;
return *this;
}
mint& operator*=(const mint &r){
val *= r.val;
val %= m;
return *this;
}
mint& operator/=(const mint &r){
assert(r != 0);
return *this = *this * r.inv();
}
friend mint operator+(const mint& l,const mint& r){
mint ans = l;
ans += r;
return ans;
}
friend mint operator-(const mint& l,const mint& r){
mint ans = l;
ans -= r;
return ans;
}
friend mint operator*(const mint& l,const mint& r){
mint ans = l;
ans *= r;
return ans;
}
friend mint operator/(const mint& l,const mint& r){
mint ans = l;
ans /= r;
return ans;
}
friend bool operator==(const mint& l,const mint& r){
return (l.val == r.val);
}
friend bool operator!=(const mint& l,const mint& r){
return (l.val != r.val);
}
int get_int(void){
return val;
}
friend ostream& operator<<(ostream& tp,const ococo_static_modint& x){
return tp << x.val;
}
friend istream& operator>>(istream& tp, ococo_static_modint &x){
ll v;
tp >> v;
x = v;
return tp;
}
};
ll beki(ll a,ll b,ll m){
ll ans = 1,temp = a % m;
while(b){
if(b % 2){
ans *= temp;
ans %= m;
}
temp *= temp;
temp %= m;
b /= 2;
}
return ans;
}
#define MULTI_TEST_CASE false
void solve(void){
//問題を見たらまず「この問題設定から言えること」をいっぱい言う
//よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く
//複数の解法のアイデアを思いついた時は全部メモしておく
//g++ -D_GLIBCXX_DEBUG -Wall -O2 f.cpp -o o
int n;
cin >> n;
ll m;
cin >> m;
if(n % 3 != 0){
EXIT_ANS(0);
return;
}
vector<vector<ll>> dp(n / 3 + 1,vector<ll>(n / 3 + 1,0));
dp[1][1] = 1;
for(int i = 2; i <= n / 3; i++){
//dp[i][1] を計算する
for(int j = 1; j <= n / 3; j++){
dp[i][1] += dp[i - 1][j] * (ll)(j + 1);
dp[i][1] %= m;
}
for(int j = 2; j <= n / 3; j++){
for(int l = 1; l < i; l++){
dp[i][j] += dp[l][1] * dp[i - l][j - 1];
dp[i][j] %= m;
}
}
}
ll ans = SUM(dp[n / 3]) % m;
cout << ans << el;
return;
}
void calc(void){
return;
}
signed main(void){
cin.tie(nullptr);
ios::sync_with_stdio(false);
calc();
int t = 1;
if(MULTI_TEST_CASE)cin >> t;
while(t--){
solve();
}
return 0;
}
ococonomy1