QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#46021 | #4422. Two Permutations | miaomiaozi | ML | 0ms | 0kb | C++17 | 3.1kb | 2022-08-24 21:02:10 | 2022-08-24 21:02:12 |
Judging History
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;
cin >> n;
vector <int> a(n + 1), b(n + 1), c(n * 2 + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int j = 1; j <= n; j++) {
cin >> b[j];
}
for (int i = 1; i <= n * 2; i++) {
cin >> c[i];
}
vector <Z> f(n * 2 + 1);
f[0] = 1;
vector <vector<tuple<int, int, Z>>> g(n * 2 + 1);
g[0] = {{0, 0, 1}};
vector pos(n + 1, vector<int>(2));
for (int i = 1; i <= n; i++) {
pos[a[i]][0] = i;
pos[b[i]][1] = i;
}
for (int i = 1; i <= n * 2; i++) {
int a = pos[c[i]][0], b = pos[c[i]][1];
for (auto &[u, v, F] : g[i - 1]) {
if (u + 1 == a) {
f[i] += F;
g[i].pb({a, v, F});
}
if (v + 1 == b) {
f[i] += F;
g[i].pb({u, b, F});
}
}
}
cout << f[n * 2] << 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: 0
Memory Limit Exceeded
input:
282 1000 434 697 735 906 614 835 227 800 116 98 776 601 110 371 262 452 608 368 719 717 241 572 203 410 440 749 604 457 516 637 488 691 841 493 418 307 745 499 833 789 819 179 357 288 129 954 29 391 80 389 771 613 653 747 928 570 518 1000 547 587 727 778 669 554 426 899 256 681 515 532 409 677 533 3...