QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#113136 | #4436. Link with Bracket Sequence II | Tobo# | AC ✓ | 282ms | 4140kb | C++20 | 1.4kb | 2023-06-16 15:06:53 | 2023-06-16 15:06:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define mod 1'000'000'007
void solve()
{
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
if (n & 1)
{
cout << 0 << '\n';
return;
}
vector<vector<int>> dp(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++)
dp[i][i - 1] = 1;
auto check1 = [&](int x, int y) -> int
{
if (a[x] < 0 || a[y] > 0)
return 0;
if (a[x] && a[y] && a[x] + a[y])
return 0;
int res = 1;
if (!a[x] && !a[y])
res = m;
return res;
};
for (int len = 2; len <= n; len += 2)
{
for (int i = 1; i + len - 1 <= n; i++)
{
dp[i][i + len - 1] = (i64)check1(i, i + len - 1) * dp[i + 1][i + len - 2] % mod;
for (int k = i + 1; k < i + len - 1; k += 2)
{
int tmp = (i64)dp[k + 1][i + len - 1] * dp[i + 1][k - 1] % mod;
tmp = (i64)tmp * check1(i, k) % mod;
dp[i][i + len - 1] = (dp[i][i + len - 1] + tmp) % mod;
}
}
}
cout << dp[1][n] << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 282ms
memory: 4140kb
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