結果

問題 No.2986 Permutation Puzzle
ユーザー Kude
提出日時 2024-12-11 01:36:18
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,835 bytes
コンパイル時間 4,053 ms
コンパイル使用メモリ 280,308 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-11 01:36:24
合計ジャッジ時間 5,353 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using A = array<int, 10>;
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
int a[10][10]{}, b[10][10]{};
rep(i, n) rep(j, n) cin >> a[i][j], a[i][j]--;
rep(i, n) rep(j, n) cin >> b[i][j], b[i][j]--;
vector<tuple<int, int, A>> hist;
A pr, pc;
rep(i, 10) pr[i] = i, pc[i] = i;
vector<P> ans;
auto dfs = [&](auto&& self, int rest) -> void {
bool is_same = [&]() {
rep(i, n) rep(j, n) if (a[pr[i]][pc[j]] != b[i][j]) return false;
return true;
}();
if (is_same) {
while (hist.size()) {
auto [rc, now, p] = hist.back(); hist.pop_back();
int cnt = 1;
bool visited[10]{};
rep(i, n) if (!visited[i]) {
int c = 0;
while (!visited[i]) visited[i] = true, c++, i = p[i];
cnt = lcm(c, cnt);
}
A inv;
rep(i, n) inv[p[i]] = i;
rep(_, cnt - 1) {
ans.emplace_back(rc, now);
if (rc == 0) {
A npr{};
rep(j, n) npr[a[pr[now]][pc[j]]] = j;
auto pre = pr;
rep(i, n) pr[i] = pre[npr[i]];
} else {
A npc{};
rep(i, n) npc[a[pr[i]][pc[now]]] = i;
auto pre = pc;
rep(j, n) pc[j] = pre[npc[j]];
}
now = inv[now];
}
}
rep(i, n) assert(pr[i] == i && pc[i] == i);
cout << ans.size() << '\n';
for (auto [rc, i] : ans) {
cout << "RC"[rc] << ' ' << i + 1 << '\n';
}
exit(0);
}
if (rest == 0) return;
rep(i, n) {
A npr{};
rep(j, n) npr[a[pr[i]][pc[j]]] = j;
auto pre = pr;
rep(i, n) pr[i] = pre[npr[i]];
hist.emplace_back(0, a[pre[i]][pc[i]], npr);
self(self, rest - 1);
hist.pop_back();
pr = pre;
}
rep(j, n) {
A npc{};
rep(i, n) npc[a[pr[i]][pc[j]]] = i;
auto pre = pc;
rep(j, n) pc[j] = pre[npc[j]];
hist.emplace_back(1, a[pr[j]][pre[j]], npc);
self(self, rest - 1);
hist.pop_back();
pc = pre;
}
};
dfs(dfs, k);
assert(false);
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0