QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#114489#4436. Link with Bracket Sequence IILiufAC ✓461ms5296kbC++201.1kb2023-06-22 10:39:482023-06-22 10:39:51

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-22 10:39:51]
  • 评测
  • 测评结果:AC
  • 用时:461ms
  • 内存:5296kb
  • [2023-06-22 10:39:48]
  • 提交

answer

#include<bits/stdc++.h>

#define endl '\n'
#define pii pair<int, int>
#define int long long

using namespace std;

const int mod=1e9+7;

void solve(){
	int n,m;
	cin>>n>>m;

	vector<int>a(n+1);
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}

	vector dp(n+10,vector<int>(n+10,-1));

	function<int(int,int)>dfs=[&](int l,int r){
		if((r-l+1)&1)return 0ll;
		if(l>r)return 1ll;
		if(dp[l][r]!=-1)return dp[l][r];
		int ans=0;
		if(a[l]>0){
			for(int i=l+1;i<=r;i+=2){
				if(a[i]==-a[l]){
					ans+=dfs(l+1,i-1)*dfs(i+1,r)%mod;
					ans%=mod;
				} else if(!a[i]){
					ans+=dfs(l+1,i-1)*dfs(i+1,r)%mod;
					ans%=mod;
				}
			}
		} else if(!a[l]){
			for(int i=l+1;i<=r;i+=2){
				if(!a[i]){
					ans+=m*dfs(l+1,i-1)%mod*dfs(i+1,r)%mod;
					ans%=mod;
				} else if(a[i]<0){
					ans+=dfs(l+1,i-1)*dfs(i+1,r)%mod;
					ans%=mod;
				}
			}
		}

		return dp[l][r]=ans;
	};

	cout<<dfs(1,n)<<endl;
	// cout<<1<<endl;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin>>t;

	while(t--){
		solve();
	}

	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 461ms
memory: 5296kb

input:

20
10 1
1 -1 0 -1 -1 1 -1 1 0 0
10 2
0 1 1 -2 1 -2 -1 1 -2 1
8 5
0 0 4 0 0 2 -2 0
9 5
0 0 0 -3 0 0 0 0 0
8 5
0 1 0 0 0 0 0 0
498 249013689
239722195 0 0 0 -59682797 187213467 0 0 220688278 0 0 -133178217 165866643 -165866643 216987003 55229518 -55229518 -216987003 0 82546192 0 0 0 0 -62330427 -19687...

output:

0
0
75
0
1125
469841384
200768531
102789125
188155310
573855452
1
10742885
839674900
273705999
280134765
397511344
679455456
227852148
343052576
776801212

result:

ok 20 lines