QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#93803 | #4422. Two Permutations | WTR2007 | WA | 817ms | 57544kb | C++20 | 2.7kb | 2023-04-02 17:24:14 | 2023-04-02 17:24:18 |
Judging History
answer
#include<bits/stdc++.h>
#define MULT_TEST 1
#define int long long
using namespace std;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int base = 137;
const int MOD = 998244353;
inline int read() {
int w = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
w = (w << 1) + (w << 3) + ch - 48;
ch = getchar();
}
return w * f;
}
inline void Solve() {
int n, ans = 0;
bool flag = 0;
n = read();
vector<int> p(n + 1), q(n + 1), h1(n + 1);
vector<int> h2(2 * n + 1), s(2 * n + 1), pom(2 * n + 1);
vector<vector<int>> dp(n + 1, vector<int>(3, 0)), pos(n + 1, vector<int>(3, 0));
for (int i = 1; i <= n; i++) p[i] = read();
for (int i = 1; i <= n; i++) {
q[i] = read();
h1[i] = (h1[i - 1] * base + q[i]) % MOD;
}
for (int i = 1; i <= 2 * n; i++) {
s[i] = read();
h2[i] = (h2[i - 1] * base + s[i]) % MOD;
if (pos[s[i]][1]) flag = 1;
else if (pos[s[i]][0]) pos[s[i]][1] = i;
else pos[s[i]][0] = i;
}
pom[0] = 1;
for (int i = 1; i <= 2 * n; i++) pom[i] = pom[i - 1] * base % MOD;
if (flag) {
printf("0\n");
return ;
}
auto Hash1 = [&](int l, int r) {
if (l > r || l < 0) return -1ll;
return (h1[r] - h1[l - 1] * pom[r - l + 1] % MOD + MOD) % MOD;
};
auto Hash2 = [&](int l, int r) {
if (l > r || l < 0) return -1ll;
return (h2[r] - h2[l - 1] * pom[r - l + 1] % MOD + MOD) % MOD;
};
if (Hash1(1, pos[p[1]][0] - 1) == Hash2(1, pos[p[1]][0] - 1)) dp[1][0] = 1;
if (Hash1(1, pos[p[1]][1] - 1) == Hash2(1, pos[p[1]][1] - 1)) dp[1][1] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j <= 1; j++) {
for (int k = 0; k <= 1; k++) {
auto Check = [&](int x, int y) {
if (x > y) return 0;
if (Hash1(x - i + 1, y - i - 1) == Hash2(x + 1, y - 1)) return 1;
else return 0;
};
if (Check(pos[p[i]][j], pos[p[i + 1]][k])) dp[i + 1][k] = (dp[i + 1][k] + dp[i][j]) % MOD;
}
}
}
vector<int> K = pos[p[n]];
if (K[0] < 2 * n && Hash2(K[0] + 1, 2 * n) == Hash1(K[0] - n + 1, n)) ans = (ans + dp[n][0]) % MOD;
if (K[1] < 2 * n && Hash2(K[1] + 1, 2 * n) == Hash1(K[1] - n + 1, n)) ans = (ans + dp[n][1]) % MOD;
printf("%lld\n", ans);
}
signed main() {
int T = 1;
#if MULT_TEST
T = read();
#endif
while (T--) Solve();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 817ms
memory: 57544kb
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 0 2 0 1 8 8 4 4 2 2 2 0 2 0 0 0 2 0 0 0 8 1 0 0 4 0 2 0 2 2 0 2 0 2 0 0 1 4 1 4 0 0 0 0 0 0 0 4 0 0 4 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 0...
result:
wrong answer 5th lines differ - expected: '2', found: '0'