QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#340539#4436. Link with Bracket Sequence IITerac#AC ✓196ms7960kbC++142.2kb2024-02-29 09:54:382024-02-29 09:54:38

Judging History

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

  • [2024-02-29 09:54:38]
  • 评测
  • 测评结果:AC
  • 用时:196ms
  • 内存:7960kb
  • [2024-02-29 09:54:38]
  • 提交

answer

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

namespace IO {
	#if ONLINE_JUDGE
	#define getc() (IS == IT && (IT = (IS = ibuf) + fread(ibuf, 1, IL, stdin), IS == IT) ? EOF : *IS++)
	#else
	#define getc() getchar()
	#endif
	const int IL = 1 << 21, OL = 1 << 21;
	int olen = 0;
	char ibuf[IL], *IS = ibuf, *IT = ibuf, obuf[OL];
	inline int read() {
		register char ch = getc(); register int x = 0, f = 1;
		while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getc(); }
		while(isdigit(ch)) x = x * 10 + ch - 48, ch = getc();
		return x * f;
	}
	inline double readdb() {
		register char ch = getc(); register double x = 0, f = 1;
		while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getc(); }
		while(isdigit(ch)) x = x * 10 + ch - 48, ch = getc();
		if(ch == '.') {
			register double b = 0.1;
			ch = getc();
			while(isdigit(ch)) x += (ch - 48) * b, b *= 0.1, ch = getc();
		}
		return x * f;
	}
	inline int readstr(char *s) {
		register char ch = getc(); register int len = 0;
		while(!isalpha(ch)) ch = getc();
		while(isalpha(ch)) s[++len] = ch, ch = getc();
		return len;
	}
	inline void flush() { fwrite(obuf, 1, olen, stdout); olen = 0; }
	inline void putc(register char ch) { obuf[olen++] = ch; }
	template<class T>
	inline void write(register T x) {
		if(x < 0) obuf[olen++] = '-', x = -x;
		if(x > 9) write(x / 10);
		obuf[olen++] = x % 10 + 48;
	}
} using namespace IO;
const int N = 5e2 + 10, mod = 1e9 + 7;
int T;
int n, m, a[N], dp[N][N];
inline int add(int x, int y) {
	return x + y >= mod ? x + y - mod : x + y;
}
bool check(int i, int j) {
	if(a[i] < a[j]) return 0;
	if(!a[i] || !a[j]) return 1;
	if(a[i] == -a[j]) return 1;
	return 0;
}
void MAIN() {
	n = read(), m = read();
	for(int i = 1; i <= n; i++)
		a[i] = read();
	if(n & 1) {
		puts("0");
		return;
	}
	memset(dp, 0, sizeof(dp));
	for(int i = 1; i <= n; i++)
		dp[i + 1][i] = 1;
	for(int len = 2; len <= n; len += 2) {
		for(int l = 1; l + len - 1 <= n; l++) {
			int r = l + len - 1;
			for(int k = l + 1; k <= r; k += 2)
				if(check(l, k)) dp[l][r] = add(dp[l][r], 1ll * ((!a[l] && !a[k]) ? m : 1) * dp[l + 1][k - 1] % mod * dp[k + 1][r] % mod);
		}
	}
	printf("%d\n", dp[1][n]);
}
int main() {
	T = read();
	while(T--) MAIN();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 196ms
memory: 7960kb

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