QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#46101#4436. Link with Bracket Sequence IImiaomiaoziAC ✓1312ms4432kbC++173.2kb2022-08-25 21:14:492022-08-25 21:14:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-25 21:14:51]
  • 评测
  • 测评结果:AC
  • 用时:1312ms
  • 内存:4432kb
  • [2022-08-25 21:14:49]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
// https://space.bilibili.com/672346917

#ifndef LOCAL
#define LOG(...) 42
#endif

#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long LL;
typedef pair <int, int> PII;

constexpr int inf = 0x3f3f3f3f;
constexpr double EPS = 1e-8;
const double PI = acos(-1);

int multi_cases = 1;

constexpr int P = 1000000007;
// constexpr int P = 998244353;

// assume -p <= x < 2P
int norm(int x) {
    if (x < 0) { x += P; }
    if (x >= P) { x -= P; }
    return x;
}
template<class T>
T power(T a, int b) {
    T res = 1;
    for (; b; b >>= 1, a *= a)  if (b & 1) res *= a;
    return res;
}
struct Z {
    int x;
    Z(int x = 0) : x(norm(x)) {}
    int val() const { return x; }
    Z operator-() const { return Z(norm(P - x)); }
    Z inv() const { assert(x != 0); return power(*this, P - 2); }
    Z &operator*=(const Z &rhs) { x = (long long)(x) * rhs.x % P; return *this; }
    Z &operator+=(const Z &rhs) { x = norm(x + rhs.x); return *this; }
    Z &operator-=(const Z &rhs) { x = norm(x - rhs.x); return *this; }
    Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
    friend Z operator*(const Z &lhs, const Z &rhs) { Z res = lhs; res *= rhs; return res; }
    friend Z operator+(const Z &lhs, const Z &rhs) { Z res = lhs; res += rhs; return res; }
    friend Z operator-(const Z &lhs, const Z &rhs) { Z res = lhs; res -= rhs; return res; }
    friend Z operator/(const Z &lhs, const Z &rhs) { Z res = lhs; res /= rhs; return res; }
    friend bool operator==(const Z &lhs, const Z &rhs) { return lhs.val() == rhs.val(); }
    friend istream &operator >> (istream &input, Z &o) {  input >> o.x; return input; }
    friend ostream &operator << (ostream &output, const Z &o) { output << o.val(); return output; }
};

void A_SOUL_AvA () {
    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 << endl;
        return;
    }

    vector f(n + 5, vector<int>(n + 5, -1));

    function <int(int, int)> DFS = [&](int l, int r) -> int {
        if ((r - l + 1) % 2) return 0;
        if (l > r) return 1;
        if (~f[l][r]) return f[l][r];

        Z ans = 0;
        int i = l, j = r;
        for (int k = i + 1; k <= j; k ++) {
            if (a[i] > 0) {
                if (a[k] == 0 || a[k] < 0 && a[k] + a[i] == 0) {
                    ans += Z(1) * DFS(i + 1, k - 1) * DFS(k + 1, j);
                }
            } else if (a[i] == 0) {
                if (a[k] == 0) {
                    ans += Z(1) * DFS(i + 1, k - 1) * m * DFS(k + 1, j);
                } else if (a[k] < 0) {
                    ans += Z(1) * DFS(i + 1, k - 1) * DFS(k + 1, j);
                }
            }
        }
        return f[l][r] = ans.val();
    };

    cout << DFS(1, n) << endl;
}

int main () {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(12);

    int T = 1;
    for (multi_cases && cin >> T; T; T--) {
        A_SOUL_AvA ();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1312ms
memory: 4432kb

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