QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#46029 | #4422. Two Permutations | miaomiaozi | AC ✓ | 1071ms | 139588kb | C++17 | 3.1kb | 2022-08-24 21:33:42 | 2022-08-24 21:33:43 |
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 vis(2 * n + 1, vector (2, false));
vector f(2 * n + 1, vector (2, Z(0)));
function <Z (int, int, int)> dfs = [&](int x, int y, int k) {
if (x > n && y > n) return Z(1);
int pos = x + y - 1;
if (vis[pos][k]) return f[pos][k];
vis[pos][k] = 1;
Z ans = 0;
if (x <= n && c[pos] == a[x]) {
ans += dfs(x + 1, y, 0);
}
if (y <= n && c[pos] == b[y]) {
ans += dfs(x, y + 1, 1);
}
return f[pos][k] = ans;
};
Z ans = 0;
if (a[1] == c[1]) ans += dfs(2, 1, 0);
if (b[1] == c[1]) ans += dfs(1, 2, 1);
cout << ans << 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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1071ms
memory: 139588kb
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...
output:
4 2 16 2 2 2 1 1 8 8 4 4 2 2 2 1 2 2 2 2 2 1 1 2 8 1 2 2 4 2 2 1 2 2 8 2 2 2 4 4 1 4 1 4 4 1 1 8 4 0 4 4 1 2 4 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
ok 282 lines