QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#88942 | #5820. 置换 | phtniit | 0 | 0ms | 0kb | C++14 | 10.2kb | 2023-03-18 00:19:34 | 2023-03-18 00:19:38 |
Judging History
answer
#include <bits/stdc++.h>
namespace atcoder {
constexpr int P = 998244353;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
template<class T>
T fpower(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
struct Z {
int x;
Z(int x = 0) : x(norm(x)) {}
Z(i64 x) : x(norm(x%P)) {}
int val() const {
return x;
}
Z operator-() const {
return Z(x == 0 ? 0 : P-x);
//return Z(norm(P - x));
}
Z inv() const {
assert(x != 0);
return fpower(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = i64(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x += rhs.x;
if (x >= P) x -= P;
//x = norm(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x -= rhs.x;
if (x < 0) x += P;
//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 std::istream &operator>>(std::istream &is, Z &a) {
i64 v;
is >> v;
a = Z(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Z &a) {
return os << a.val();
}
};
std::vector<int> rev;
std::vector<Z> roots{0, 1};
void dft(std::vector<Z> &a) {
int n = a.size();
if (int(rev.size()) != n) {
int k = __builtin_ctz(n) - 1;
rev.resize(n);
for (int i = 0; i < n; i++) {
rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
}
}
for (int i = 0; i < n; i++) {
if (rev[i] < i) {
std::swap(a[i], a[rev[i]]);
}
}
if (int(roots.size()) < n) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e = fpower(Z(3), (P - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots[2 * i] = roots[i];
roots[2 * i + 1] = roots[i] * e;
}
k++;
}
}
for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}
void idft(std::vector<Z> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
Z inv = (1 - P) / n;
for (int i = 0; i < n; i++) {
a[i] *= inv;
}
}
struct Poly {
std::vector<Z> a;
Poly() {}
Poly(const std::vector<Z> &a) : a(a) {}
Poly(const std::initializer_list<Z> &a) : a(a) {}
int size() const {
return a.size();
}
void resize(int n) {
a.resize(n);
}
Z operator[](int idx) const {
if (idx < size()) {
return a[idx];
} else {
return 0;
}
}
Z &operator[](int idx) {
return a[idx];
}
Poly mulxk(int k) const {
auto b = a;
b.insert(b.begin(), k, 0);
return Poly(b);
}
Poly modxk(int k) const {
k = std::min(k, size());
return Poly(std::vector<Z>(a.begin(), a.begin() + k));
}
Poly divxk(int k) const {
if (size() <= k) {
return Poly();
}
return Poly(std::vector<Z>(a.begin() + k, a.end()));
}
friend Poly operator+(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] + b[i];
}
return Poly(res);
}
friend Poly operator-(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] - b[i];
}
return Poly(res);
}
friend Poly operator-(const Poly &a) {
std::vector<Z> res(a.size());
for (int i = 0; i < int(res.size()); i++) {
res[i] = -a[i];
}
return Poly(res);
}
friend Poly operator*(Poly a, Poly b) {
if (a.size() == 0 || b.size() == 0) {
return Poly();
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) {
sz *= 2;
}
a.a.resize(sz);
b.a.resize(sz);
dft(a.a);
dft(b.a);
for (int i = 0; i < sz; ++i) {
a.a[i] = a[i] * b[i];
}
idft(a.a);
a.resize(tot);
return a;
}
friend Poly operator*(Z a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}
friend Poly operator*(Poly a, Z b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}
Poly &operator+=(Poly b) {
return (*this) = (*this) + b;
}
Poly &operator-=(Poly b) {
return (*this) = (*this) - b;
}
Poly &operator*=(Poly b) {
return (*this) = (*this) * b;
}
Poly deriv() const {
if (a.empty()) {
return Poly();
}
std::vector<Z> res(size() - 1);
for (int i = 0; i < size() - 1; ++i) {
res[i] = (i + 1) * a[i + 1];
}
return Poly(res);
}
Poly integr() const {
std::vector<Z> res(size() + 1);
for (int i = 0; i < size(); ++i) {
res[i + 1] = a[i] / (i + 1);
}
return Poly(res);
}
Poly inv(int m) const {
Poly x{a[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
}
return x.modxk(m);
}
Poly log(int m) const {
return (deriv() * inv(m)).integr().modxk(m);
}
Poly exp(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{1} - x.log(k) + modxk(k))).modxk(k);
}
return x.modxk(m);
}
Poly pow(int k, int m) const {
int i = 0;
while (i < size() && a[i].val() == 0) {
i++;
}
if (i == size() || 1LL * i * k >= m) {
return Poly(std::vector<Z>(m));
}
Z v = a[i];
auto f = divxk(i) * v.inv();
return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * fpower(v, k);
}
Poly sqrt(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((P + 1) / 2);
}
return x.modxk(m);
}
Poly mulT(Poly b) const {
if (b.size() == 0) {
return Poly();
}
int n = b.size();
std::reverse(b.a.begin(), b.a.end());
return ((*this) * b).divxk(n - 1);
}
std::vector<Z> eval(std::vector<Z> x) const {
if (size() == 0) {
return std::vector<Z>(x.size(), 0);
}
const int n = std::max(int(x.size()), size());
std::vector<Poly> q(4 * n);
std::vector<Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
q[p] = Poly{1, -x[l]};
} else {
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
if (r - l == 1) {
if (l < int(ans.size())) {
ans[l] = num[0];
}
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};
namespace simp {
std::vector<Z> fac, ifac, invn;
void check(int x) {
if (fac.empty()) {
fac={Z(1), Z(1)};
ifac={Z(1), Z(1)};
invn={Z(0), Z(1)};
}
while (fac.size()<=x) {
int n = fac.size(), m = fac.size() * 2;
fac.resize(m);
ifac.resize(m);
invn.resize(m);
for (int i=n;i<m;i++) {
fac[i]=fac[i-1]*Z(i);
invn[i]=Z(P-P/i)*invn[P%i];
ifac[i]=ifac[i-1]*invn[i];
}
}
}
Z gfac(int x) {
check(x); return fac[x];
}
Z ginv(int x) {
check(x); return invn[x];
}
Z gifac(int x) {
check(x); return ifac[x];
}
Z binom(int n,int m) {
if (m < 0 || m > n) return Z(0);
return gfac(n)*gifac(m)*gifac(n - m);
}
}
}
inline atcoder::Z C(int n, int m) {
return atcoder::simp::binom(n, m);
}
inline atcoder::Z F(int n) {
return atcoder::simp::gfac(n);
}
inline atcoder::Z iF(int n) {
return atcoder::simp::gifac(n);
}
inline atcoder::Z fpow(long long a, long long b) {
return atcoder::fpower(atcoder::Z{a}, b);
}
using namespace std;
using i64 = long long;
const int maxn = 1000050;
vector<int> D[maxn];
void once() {
int n, k;
cin >> n >> k;
static int a[3030], cnt[3030];
for (int i = 1; i <= n; ++i) {
cin >> a[i];
cnt[i] = 0;
}
map<int, int> M;
for (int i = 1; i <= n; ++i) if (!cnt[i]) {
int num = 0;
for (int j = i; cnt[j] == 0; j = a[j]) {
num++;
cnt[j] = 1;
}
M[num]++;
}
atcoder::Z ans = 1;
for (auto e: M) {
static int T = 0;
std::function<atcoder::Z(int)> dfs = [&](int c) {
if (c == 0) {
return atcoder::Z{1};
}
static atcoder::Z dp[3030];
static int vis[3030];
if (vis[c] == T) {
return dp[c];
}
vis[c] = T;
atcoder::Z res = 0;
for (auto d: D[k]) {
if (d > c) {
break;
}
if (__gcd(e.first, k/d) != 1) {
continue;
}
res += C(c-1, d-1) * F(d-1) * fpow(e.first, d-1) * dfs(c - d);
}
return dp[c] = res;
};
++T;
auto res = dfs(e.second);
if (res.x == 0) {
puts("0");
return;
}
ans *= res;
}
cout << ans << endl;
}
int main() {
const int lim = 1000000;
for (int i = 1; i <= lim; ++i) {
for (int j = i; j <= lim; j += i) {
D[j].emplace_back(i);
}
}
int tes;
cin >> tes;
while (tes--) {
once();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
10 6 5 1 2 6 3 4 5 5 8 1 2 3 4 5 7 5 1 2 3 4 5 6 7 4 4 1 2 3 4 7 7 1 2 3 4 5 6 7 4 4 1 2 3 4 5 4 1 2 4 3 5 8 8 1 2 3 4 5 6 7 8 4 5 1 3 2 4 6 6 1 2 3 4 5 6
output:
result:
Test #2:
score: 0
Time Limit Exceeded
input:
10 4 6 3 1 2 4 5 8 2 1 3 4 5 8 4 1 2 3 4 5 6 7 8 6 5 4 1 2 3 5 6 5 5 1 2 3 4 5 5 5 1 2 3 4 5 6 7 1 2 3 4 5 6 6 4 1 2 3 4 5 6 6 7 6 1 2 3 4 5 7 6 1 2 3 4 5 6 7
output:
result:
Test #3:
score: 0
Time Limit Exceeded
input:
10 6 602552 1 2 3 4 5 6 4 775694 1 2 4 3 6 668467 1 4 2 3 5 6 6 558385 1 2 6 3 4 5 7 832183 4 1 2 3 5 6 7 6 631375 1 2 3 4 5 6 8 519340 1 2 3 5 4 6 7 8 4 636124 1 2 3 4 4 759099 3 1 2 4 7 977752 1 2 3 4 5 6 7
output:
result:
Test #4:
score: 0
Time Limit Exceeded
input:
10 43 725761 1 2 3 4 5 6 7 8 10 9 11 12 13 15 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 37 542860 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 27 26 28 29 30 31 32 33 34 36 35 37 27 793967 2 1 4 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
output:
result:
Test #5:
score: 0
Time Limit Exceeded
input:
10 40 535121 3 1 2 4 5 6 7 8 9 11 10 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 43 660193 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 15 19 17 18 20 26 21 22 23 24 25 27 28 29 30 34 31 32 33 35 36 37 38 39 40 41 42 43 38 596459 1 2 4 3 5 6 7 8 9 10 11 12 13 15 14 ...
output:
result:
Test #6:
score: 0
Time Limit Exceeded
input:
10 33 892596 1 2 6 3 4 5 9 7 8 10 11 12 14 13 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 32 30 31 33 39 875634 1 2 10 3 4 5 6 7 8 9 11 12 13 14 16 15 17 18 20 19 21 22 23 24 26 25 27 28 29 30 31 32 33 35 34 36 37 38 39 27 856117 1 2 3 4 5 10 6 7 8 9 11 12 13 14 15 16 17 18 20 19 21 24 22 23 25 26 ...
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
10 2899 540778 3 1 2 4 5 6 9 7 8 10 11 12 13 14 15 16 17 18 19 20 21 22 24 23 27 25 26 28 29 30 31 32 33 34 35 37 36 38 39 40 41 42 44 43 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 70 68 69 71 72 73 74 75 76 77 78 80 79 81 82 83 89 84 85 86 87 88 90 91 92 93 94 95 96 97 98 ...
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
10 2310 568163 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26 24 25 27 28 30 29 31 32 33 34 35 36 37 38 39 40 41 46 42 43 44 45 47 48 49 50 51 52 57 53 54 55 56 58 59 61 60 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
10 1564 511376 1 2 3 4 5 6 8 7 9 10 11 12 13 14 15 16 20 17 18 19 21 22 23 24 25 26 28 27 29 30 31 35 32 33 34 36 41 37 38 39 40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 76 74 75 77 78 82 79 80 81 83 84 85 86 87 88 89 90 91 92 93 94 97 95 96 98 ...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
10 1749 969801 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 34 31 32 33 36 35 37 38 39 40 41 42 43 44 45 46 48 47 49 50 51 54 52 53 55 56 57 58 60 59 61 62 63 64 65 66 67 68 69 70 71 72 73 75 74 76 77 78 79 80 81 82 83 84 85 86 89 87 88 90 91 92 93 94 95 96 97 99 ...