結果
| 問題 |
No.2384 Permutations of Permutations
|
| コンテスト | |
| ユーザー |
noshi91
|
| 提出日時 | 2023-05-18 00:02:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 2,105 bytes |
| コンパイル時間 | 2,089 ms |
| コンパイル使用メモリ | 198,036 KB |
| 最終ジャッジ日時 | 2025-02-13 01:09:10 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/modint>
using mint = atcoder::modint998244353;
using S6 = std::array<int, 6>;
S6 op(S6 x, const S6 &y) {
for (int i = 0; i < 6; i++)
x[i] = y[x[i]];
return x;
}
S6 inv(const S6 &x) {
S6 y;
for (int i = 0; i < 6; i++)
y[x[i]] = i;
return y;
}
int index(S6 p) {
S6 q = inv(p);
int ret = 0;
for (int i = 6; i-- > 1;) {
ret = ret * (i + 1) + p[i];
int next = p[i], prev = q[i];
p[prev] = next, q[next] = prev;
}
return ret;
}
mint solve(const int N, const int K) {
if (N == 2) {
return 1;
}
if (N != 6) {
mint ret = K;
for (int i = 1; i <= N - K; i++)
ret *= i;
return ret;
}
const S6 id = {{0, 1, 2, 3, 4, 5}};
// one of the outer automorphisms
std::array<int, 720> out;
{
const std::array<S6, 5> adj = {{
{1, 0, 3, 2, 5, 4},
{5, 3, 4, 1, 2, 0},
{3, 2, 1, 0, 5, 4},
{5, 4, 3, 2, 1, 0},
{2, 3, 0, 1, 5, 4},
}};
S6 p = id;
do {
S6 p_ = p, q = id;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (p_[j] > p_[j + 1]) {
std::swap(p_[j], p_[j + 1]);
q = op(q, adj[j]);
}
}
}
out[index(p)] = index(q);
} while (std::next_permutation(p.begin(), p.end()));
}
S6 f = id;
std::rotate(f.begin(), f.begin() + 1, f.begin() + K);
// target[F(f)] must be true
std::array<bool, 720> target = {};
{
S6 p = id;
do {
bool ok = true;
for (int i = 0; i < K; i++)
ok &= p[i] == f[i];
target[index(p)] = ok;
} while (std::next_permutation(p.begin(), p.end()));
}
mint ans = 0;
{
S6 p = id;
do {
S6 t = op(op(inv(p), f), p);
// check inner automorphism
if (target[index(t)])
ans += 1;
// check outer automorphism
if (target[out[index(t)]])
ans += 1;
} while (std::next_permutation(p.begin(), p.end()));
}
return ans;
}
int main() {
int N, K;
std::cin >> N >> K;
std::cout << solve(N, K).val() << "\n";
return 0;
}
noshi91