QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#41443 | #4436. Link with Bracket Sequence II | L1ngYu | AC ✓ | 1016ms | 7836kb | C++20 | 2.9kb | 2022-07-30 21:54:58 | 2022-07-30 21:54:58 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define forr(i,a) for(auto i:a)
#define rall(a) rbegin(a),rend(a)
#define all(a) begin(a),end(a)
#define pb emplace_back
using namespace std;
struct read
{
static const int M = 1 << 23;
char buf[M], *S = buf, *P = buf, c, l;
inline char gc() { return (S == P && (P = (S = buf) + fread(buf, 1, M, stdin), S == P) ? EOF : *S++); }
template<class T> read &operator>>(T &x)
{
for (c = 0;!isdigit(c);c = gc()) l = c;
for (x = 0;isdigit(c);c = gc()) x = x * 10 + (c & 15);
return x = (l ^ 45) ? x : -x, *this;
}
}Cin;
constexpr int mod = 1e9 + 7;
template<class T> inline T Norm(const T &v) { return (v % mod + mod) % mod; }
template<class T> constexpr T ksm(T a, int b, const int &p = mod, T c = 1)
{
for (a %= p;b;(a *= a) %= p, b /= 2) if (b & 1) (c *= a) %= p;
return c;
}
struct Z
{
using F = long long; F v;
Z(long long _x = 0) : v(Norm(_x)) {}
Z operator-() const { return Z(Norm(mod - v)); }
Z inv() const { return ksm(*this, mod - 2, mod); }
Z &operator*=(const Z &R) { return v = v * R.v % mod, *this; }
Z &operator+=(const Z &R) { return v = Norm(v + R.v), *this; }
Z &operator-=(const Z &R) { return v = Norm(v - R.v), *this; }
Z &operator/=(const Z &R) { return *this *= R.inv(); }
Z &operator%=(const F &R) { return v %= R, *this; }
friend Z operator*(Z L, const Z &R) { return L *= R; }
friend Z operator+(Z L, const Z &R) { return L += R; }
friend Z operator-(Z L, const Z &R) { return L -= R; }
friend Z operator/(Z L, const Z &R) { return L /= R; }
friend Z operator%(Z L, const F &R) { return L %= R; }
auto operator<=>(const Z &) const = default;
friend auto &operator>>(istream &i, Z &z) { return i >> z.v; }
friend auto &operator<<(ostream &o, const Z &z) { return o << z.v; }
};
constexpr int N = 510;
int a[N], ndp[N][N]; Z f[N][N];
void solve()
{
int n, m; Cin >> n >> m;
rep(i, 1, n) Cin >> a[i];
if (n & 1) return void(cout << "0\n");
for (int len = 2;len <= n;len += 2)
for (int i = 1, j = len;j <= n;++i, ++j)
{
if (!a[i] || !a[j])
{
if (!a[i] && !a[j]) f[i][j] = f[i + 1][j - 1] * m;
else if (a[i]) f[i][j] = f[i + 1][j - 1] * (a[i] > 0);
else f[i][j] = f[i + 1][j - 1] * (a[j] < 0);
}
else f[i][j] = f[i + 1][j - 1] * (a[i] > 0 && a[j] < 9 && a[i] + a[j] == 0);
ndp[i][j] = f[i][j].v;
for (int k = i;k <= j - 1;++k) f[i][j] += f[i][k] * ndp[k + 1][j];
}
cout << f[1][n] << '\n';
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
rep(i, 1, 505) f[i][i - 1] = ndp[i][i - 1] = 1;
int _;for (Cin >> _; _--; ) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1016ms
memory: 7836kb
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