結果

問題 No.2384 Permutations of Permutations
ユーザー noshi91noshi91
提出日時 2023-05-18 00:02:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,105 bytes
コンパイル時間 2,181 ms
コンパイル使用メモリ 204,576 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 14:01:24
合計ジャッジ時間 2,755 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0