結果

問題 No.1873 Bracket Swapping
ユーザー 沙耶花沙耶花
提出日時 2022-03-11 22:26:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 2,964 bytes
コンパイル時間 4,808 ms
コンパイル使用メモリ 274,800 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-14 07:40:45
合計ジャッジ時間 7,748 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 14 ms
4,376 KB
testcase_04 AC 7 ms
4,372 KB
testcase_05 AC 6 ms
4,368 KB
testcase_06 AC 61 ms
4,368 KB
testcase_07 AC 1 ms
4,372 KB
testcase_08 AC 113 ms
4,372 KB
testcase_09 AC 114 ms
4,372 KB
testcase_10 AC 85 ms
4,368 KB
testcase_11 AC 3 ms
4,372 KB
testcase_12 AC 46 ms
4,372 KB
testcase_13 AC 38 ms
4,368 KB
testcase_14 AC 112 ms
4,368 KB
testcase_15 AC 25 ms
4,372 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 59 ms
4,372 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 5 ms
4,372 KB
testcase_20 AC 90 ms
4,372 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 9 ms
4,372 KB
testcase_23 AC 151 ms
4,368 KB
testcase_24 AC 104 ms
4,372 KB
testcase_25 AC 119 ms
4,372 KB
testcase_26 AC 58 ms
4,372 KB
testcase_27 AC 85 ms
4,368 KB
testcase_28 AC 11 ms
4,372 KB
testcase_29 AC 2 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define Inf 1000000001

template <class S,
          S (*op0)(S, S),
          S (*e0)(),
		  S (*op1)(S, S),
          S (*e1)()
          >
struct matrix{
	vector<vector<S>> v;
	int _h,_w;
	
	matrix(vector<vector<S>> X){
		v = X;
		_h = X.size();
		_w = 0;
		if(X.size()>0)_w = X[0].size();
	}
	
	matrix(int h,int w){
		v.resize(h,vector<S>(w,e0()));
		_h = h;
		_w = w;
	}
	
	void add_element(int from,int to,S x){
		v[to][from] = op0(v[to][from],x);
	}
	
	matrix e(){
		assert(_h==_w);
		matrix<S,op0,e0,op1,e1> ret(_h,_w);
		for(int i=0;i<_h;i++){
			for(int j=0;j<_w;j++){
				if(i==j)ret.v[i][j] = e1();
				else ret.v[i][j] = e0();
			}
		}
		return ret;
	}
	
	matrix &operator*=(const matrix &another){
		matrix<S,op0,e0,op1,e1> ret(_h,another._w);
		for(int i=0;i<_h;i++){
			for(int j=0;j<another._w;j++){
				ret.v[i][j] = e0();
				for(int k=0;k<_w;k++){
					ret.v[i][j] = op0(ret.v[i][j],op1(v[i][k],another.v[k][j]));
				}
			}
		}

		v = ret.v;
		return (*this);
	}
	
	matrix operator*(const matrix &another)const{
		return (matrix(*this)*=another);
	}
	
	matrix pow(long long cnt){
		matrix<S,op0,e0,op1,e1> ret = e();
		auto temp = *this;
		while(cnt!=0LL){
			if((cnt&1)==1){
				ret *= temp;
			}
			temp *= temp;
			cnt>>=1;
		}
		return ret;
	}
};

mint op0(mint a,mint b){
	return a+b;
}

mint e0(){
	return 0;
}

mint op1(mint a,mint b){
	return a*b;
}

mint e1(){
	return 1;
}

int main(){
	
	string s;
	cin>>s;
	int n = s.size()/2;
	int K;
	cin>>K;
	vector dp(2*n+3,vector<mint>(n+1,0));
	dp[0][0] = 1;
	
	rep(i,s.size()){
		vector ndp(2*n+3,vector<mint>(n+1,0));
		rep(j,n*2+3){
			rep(k,n+1){
				if(dp[j][k]==0)continue;
				rep(l,2){
					int jj = j,kk = k;
					if(l==0)jj++;
					else jj--;
					if(jj<0)continue;
					if(s[i]=='('&&l==0)kk++;
					ndp[jj][kk] += dp[j][k];
				}
			}
		}
		swap(dp,ndp);
	}
	matrix<mint,op0,e0,op1,e1> M(n+1,n+1);
	
	rep(i,n+1){
		vector<mint> cnt(4,0);
		cnt[0] = i;
		cnt[1] = i;
		cnt[2] = n-i;
		cnt[3] = n-i;
		rep(j,4){
			for(int k=j;k<4;k++){
				int X = i;
				int Y = i;
				mint t = 0;
				if(j==k){
					t = cnt[j];
					t *= cnt[j]-1;
					t /= 2;
					//if(i==0)cout<<t.val()<<endl;
				}
				else{
					Y *= 2;
					if((j&2)==0)Y--;
					if((k&2)==0)Y--;
					int XX = j&1;
					XX ^= (j>>1);
					int YY = k&1;
					YY ^= (k>>1);
					t = cnt[j];
					t *= cnt[k];
					if(XX == (k&1))Y++;
					if(YY == (j&1))Y++;
					Y /=2;
					//if(i==1)cout<<j<<','<<k<<','<<t.val()<<','<<Y<<endl;
				}
				//cout<<Y<<endl;
				if(t.val()!=0 && Y<=n && Y>=0)M.add_element(X,Y,t);
			}
		}
	}
	M = M.pow(K);
	
	matrix<mint,op0,e0,op1,e1> temp(n+1,1);
	rep(i,n+1){
		temp.v[i][0] = dp[0][i];
		//cout<<dp[0][i].val()<<endl;
	}
	temp = M * temp;
	mint ans = temp.v[n][0];
	cout<<ans.val()<<endl;
	
	return 0;
}
0