QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#60398#4436. Link with Bracket Sequence IIqinjianbin#AC ✓1430ms5528kbC++171.1kb2022-11-04 13:13:252022-11-04 13:13:26

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 13:13:26]
  • 评测
  • 测评结果:AC
  • 用时:1430ms
  • 内存:5528kb
  • [2022-11-04 13:13:25]
  • 提交

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(i >= j) return 0;
	if(!a[i] && !a[j]) return m;
	if(!a[i] && a[j] < 0 || !a[j] && a[i] > 0) return 1;
	if(a[i] > 0 && a[i] + a[j] == 0) return 1;
	return 0;
}

ll get(int l, int r)
{
	if(l >= r || (r - l + 1) % 2 == 1) return 0;
	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(i, 1, n) dp[i][i] = 0;
		_for(len, 2, n)
			_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("!! %d %d %lld\n", l, r, dp[l][r]);
			}
		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: 1430ms
memory: 5528kb

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