QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#41158 | #4422. Two Permutations | L1ngYu | AC ✓ | 249ms | 88380kb | C++20 | 2.8kb | 2022-07-28 13:13:37 | 2022-07-28 13:13:40 |
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;
static const int M = 1 << 23;
static char buf[M], *S = buf, *P = buf, c, l;
struct read
{
inline char gc() { return (S == P && (P = (S = buf) + fread(buf, 1, M, stdin), S == P) ? EOF : *S++); }
template<class T> inline 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 = 998244353;
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 = 1e6 + 10;
int a[N], b[N], s[N], f[2][N];
void solve()
{
int n; Cin >> n;
rep(i, 1, n) Cin >> a[i];
rep(i, 1, n) Cin >> b[i];
rep(i, 1, 2 * n) Cin >> s[i];
rep(i, 0, 1) memset(f[i], -1, 2 * (n + 1) * sizeof(f[0][0]));
function<int(int, int, bool)> dfs = [&](int x, int y, bool t) ->int
{
if (x > n && y > n) return 1;
if (~f[t][x + y - 1]) return f[t][x + y - 1];
Z ret = 0;
if (a[x] == s[x + y - 1] && x <= n) ret += dfs(x + 1, y, 0);
if (b[y] == s[x + y - 1] && y <= n) ret += dfs(x, y + 1, 1);
f[t][x + y - 1] = ret.v;
return ret.v;
};
Z ret = 0;
if (a[1] == s[1]) ret += dfs(2, 1, 0);
if (b[1] == s[1]) ret += dfs(1, 2, 1);
cout << ret << '\n';
}
signed main()
{
int _;for (Cin >> _; _--; ) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 249ms
memory: 88380kb
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