#include<bits/stdc++.h> using ll = longlong; constexprint N = 2e5 + 10; ll n, k, p, x; ll a[N], h[N]; std::map<ll, int> cnt; ll base[N], ibase[N]; ll fpow(ll b, ll p, ll m){ ll res = 1; while (p) { if (p & 1) res = res * b % m; b = b * b % m; p >>= 1; } return res; } intmain(){ #ifdef ONLINE_JUDGE std::cin.tie(0)->sync_with_stdio(0); #endif std::cin >> n >> k >> p >> x; for (int i = 1; i <= n; i++) std::cin >> a[i]; base[0] = 1; for (int i = 1; i <= n; i++) base[i] = base[i - 1] * k % p; ibase[n] = fpow(base[n], p - 2, p); for (int i = n - 1; i >= 0; i--) ibase[i] = ibase[i + 1] * k % p; h[0] = 0; ll ans = 0; cnt[0] = 1; for (int i = 1; i <= n; i++) { h[i] = (h[i - 1] * k + a[i]) % p; ll q = ((h[i] - x) % p + p) % p * ibase[i] % p; ans += cnt[q]; cnt[h[i] * ibase[i] % p]++; } std::cout << ans << "\n"; }
voidrun(){ int n; std::cin >> n; std::vector<int> a(n), b(n), c; for (int i = 0; i < n; i++) std::cin >> a[i], c.push_back(a[i]); for (int i = 0; i < n; i++) std::cin >> b[i], c.push_back(b[i]); std::sort(c.begin(), c.end());
int x = c[n - 1], y = c[n]; std::vector<int> pos; for (int i = 0; i < n; i++) if (a[i] == x || a[i] == y || b[i] == x || b[i] == y) pos.push_back(i);
if (pos.size() == 2) { int t = 0; for (auto i : pos) if (a[i] == x || a[i] == y) t++; if (t == 2 || t == 0) std::swap(a[pos[0]], b[pos[0]]); } else pos.push_back(pos[0]);
int t = (a[pos[0]] < x) + (a[pos[1]] < x); for (int i = 0; i < n; i++) { if (i == pos[0] || i == pos[1]) continue; if (a[i] < x) { if (t * 2 + 1 < n) t++; elseif (b[i] > y) std::swap(a[i], b[i]); else t++; } }
if (t * 2 + 1 < n) { for (int i = 0; i < n && t * 2 + 1 < n; i++) { if (i == pos[0] || i == pos[1]) continue; if (a[i] > y && b[i] < x) std::swap(a[i], b[i]), t++; } } if (t * 2 + 1 > n) { for (int i = 0; i < n && t * 2 + 1 > n; i++) { if (i == pos[0] || i == pos[1]) continue; if (a[i] < x && b[i] > y) std::swap(a[i], b[i]), t--; } } for (int i = 0; i < n; i++) std::cout << a[i] << " \n"[i == n - 1]; for (int i = 0; i < n; i++) std::cout << b[i] << " \n"[i == n - 1]; }
intmain(){ #ifdef ONLINE_JUDGE std::cin.tie(0)->sync_with_stdio(0); std::cout.tie(0); #endif int t; std::cin >> t; while (t--) run(); //, std::fprintf(stderr, "----------------\n"); }