QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#114488#4436. Link with Bracket Sequence IILiufAC ✓452ms5292kbC++201.4kb2023-06-22 10:36:512023-06-22 10:36:54

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:36:54]
  • 评测
  • 测评结果:AC
  • 用时:452ms
  • 内存:5292kb
  • [2023-06-22 10:36:51]
  • 提交

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) % 2)return 0ll;
    if (L > R)return 1ll;
    if (dp[L][R] != -1)return dp[L][R];
    int res = 0;
    if (a[L] == 0) {
        for (int i = L + 1; i <= R; i += 2) {
            if (a[i] == 0) {
                res += m * dfs(i + 1, R)%mod * dfs(L + 1, i - 1)%mod;
                res%=mod;
            }
            else if (a[i] < 0) {
                res+= dfs(i + 1, R) * dfs(L + 1, i - 1)%mod;
                res%=mod;             
            }
        }
    }
    else if (a[L] > 0) {
        for (int i = L + 1; i <= R; i += 2) {
            if (a[i] == 0) {
                res += dfs(i + 1, R) * dfs(L + 1, i - 1)%mod;
                res%=mod;
            }
            else if (a[i] == -a[L]) {
                res += dfs(i + 1, R) * dfs(L + 1, i - 1)%mod;
                res%=mod;
            }
        }
    }
    return dp[L][R] = res;
	};

	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: 452ms
memory: 5292kb

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