QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#66016#4436. Link with Bracket Sequence IIneko_nyaa#AC ✓279ms5252kbC++231010b2022-12-05 20:23:152022-12-05 20:23:18

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-05 20:23:18]
  • 评测
  • 测评结果:AC
  • 用时:279ms
  • 内存:5252kb
  • [2022-12-05 20:23:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int M = 1e9+7;

int n, m, a[509], dp[509][509];

int go(int l, int r) {
	if (l > r) return 1;
	if (dp[l][r] != -1) return dp[l][r];

	int ans = 0;
	for (int i = l+1; i <= r; i += 2) {
		if (a[l] == 0 && a[i] == 0) {
			int tmp = go(l+1, i-1)*go(i+1, r) % M;
			tmp = (tmp*m) % M;
			ans = (ans + tmp) % M;
		} else {
			if (a[l] > 0 && a[i] == 0) {
				ans = (ans + go(l+1, i-1)*go(i+1, r)) % M;
			} else if (a[l] == 0 && a[i] < 0) {
				ans = (ans + go(l+1, i-1)*go(i+1, r)) % M;
			} else if (a[l] > 0 && a[i] < 0 && a[l]+a[i] == 0) {
				ans = (ans + go(l+1, i-1)*go(i+1, r)) % M;
			}	
		}
	}
	return dp[l][r] = ans;
}

void solve() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	memset(dp, -1, sizeof(dp));

	cout << go(0, n-1) << '\n';
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	
	int t; cin >> t;
	while (t--) {
		solve();
	}

	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 279ms
memory: 5252kb

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