QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#60461#4436. Link with Bracket Sequence IIqinjianbinAC ✓685ms5572kbC++17960b2022-11-04 22:23:582022-11-04 22:24:01

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-04 22:24:01]
  • 评测
  • 测评结果:AC
  • 用时:685ms
  • 内存:5572kb
  • [2022-11-04 22:23:58]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i < (b); i++) 
#define _for(i, a, b) for(int i = (a); i <= (b); i++) 
using namespace std;

typedef long long ll;
const int N = 500 + 10;
const int mod = 1e9 + 7;
ll dp[N][N], m;
int a[N], n;

ll cal(int i, int j)
{
	if(a[i] < 0 || a[j] > 0) return 0;
	if(!a[i] && !a[j]) return m;
	if(!a[i] || !a[j]) return 1;
	if(a[i] + a[j] == 0) return 1;
	return 0;
}

ll get(int l, int r)
{
	if(l + 1 == r) return cal(l, r);
	return cal(l, r) * dp[l + 1][r - 1] % mod;
}

int main()
{
	int T; scanf("%d", &T);
	while(T--)
	{
		scanf("%d%lld", &n, &m);
		_for(i, 1, n) scanf("%d", &a[i]);

		for(int len = 2; len <= n; len += 2)
			_for(l, 1, n)
			{
				int r = l + len - 1;
				if(r > n) break;

				dp[l][r] = get(l, r);
				_for(k, l + 1, r - 2)
					(dp[l][r] += get(l, k) * dp[k + 1][r] % mod) %= mod;
			}
		printf("%lld\n", dp[1][n]);
	}

	return 0; 
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 685ms
memory: 5572kb

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