結果
| 問題 | No.3529 2p Teleportations |
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2026-05-05 01:26:45 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 53 ms / 2,000 ms |
| コード長 | 3,556 bytes |
| 記録 | |
| コンパイル時間 | 2,313 ms |
| コンパイル使用メモリ | 344,012 KB |
| 実行使用メモリ | 7,972 KB |
| 最終ジャッジ日時 | 2026-05-05 01:27:01 |
| 合計ジャッジ時間 | 13,407 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 49 |
ソースコード
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
// using namespace atcoder;
// using mint = modint1000000007;
// const int mod = 1000000007;
// using mint = modint998244353;
// const int mod = 998244353;
// const int INF = 1e9;
// const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, l, r) for (int i = (l); i < (r); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define P pair<int, int>
template<typename A, typename B> inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return true; } return false; }
#ifndef KWM_T_MATH_DIOPHANTINE_HPP
#define KWM_T_MATH_DIOPHANTINE_HPP
#include <utility>
#include <cassert>
namespace kwm_t::math {
/**
* @brief 拡張ユークリッドの互除法
*
* ax + by = gcd(a,b) を満たす (x,y) を返す
*/
long long ext_gcd(long long a, long long b, long long& x, long long& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long x1, y1;
long long g = ext_gcd(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
/**
* @brief 線形ディオファントス方程式 ax + by = c
*
* return:
* {x0, y0} 1つの解(存在しない場合 {-1,-1})
*
* 注意:
* ・一般解は x = x0 + t*(b/g), y = y0 - t*(a/g)
* ・x >= 0 最小化などは別処理
*/
std::pair<long long, long long>
linear_diophantine(long long a, long long b, long long c) {
long long x, y;
long long g = ext_gcd(a, b, x, y);
if (c % g != 0) return { -1, -1 };
long long mul = c / g;
x *= mul;
y *= mul;
return { x, y };
}
/**
* @brief ax ≡ c (mod b) の解(存在すれば1つ)
*
* return:
* x ≡ solution (mod b/g)
* なければ -1
* verified
* https://atcoder.jp/contests/abc423/submissions/74801048
*/
long long mod_linear_equation(long long a, long long b, long long c) {
long long x, y;
long long g = ext_gcd(a, b, x, y);
if (c % g != 0) return -1;
long long mod = b / g;
long long res = (x * (c / g)) % mod;
if (res < 0) res += mod;
return res;
}
} // namespace kwm_t::math
#endif
void solve() {
int n, p; cin >> n >> p;
vector<int>a(n);
rep(i, n) {
int x; cin >> x; x--;
a[x] = i;
}
vector<int>b(n, -1);
vector<vector<int>>keep(n + 1);
vector<bool>vis(n);
rep(i, n)if (!vis[i]) {
int v = i;
vector<int>vs;
while (!vis[v]) {
vis[v] = true;
vs.push_back(v);
v = a[v];
}
int sz = vs.size();
if (sz % 2) {
int x = kwm_t::math::mod_linear_equation(2 * p, sz, 1);
rep(i, sz) b[vs[i]] = vs[(i + x) % sz];
}
else if (keep[sz].empty()) {
keep[sz].swap(vs);
}
else {
vector<int>vs2;
vs2.swap(keep[sz]);
int x = kwm_t::math::mod_linear_equation(p, sz, 1);
rep(i, sz) {
b[vs[i]] = vs2[i];
b[vs2[i]] = vs[(i + x) % sz];
}
}
}
// 後処理
rep(i, n + 1) {
if (keep[i].empty())continue;
auto vs = keep[i];
int sz = vs.size();
int x = kwm_t::math::mod_linear_equation(2 * p, sz - 1, 1);
rep(i, sz - 1) b[vs[i]] = vs[(i + x) % (sz - 1)];
// ハブったやつの行き先を決めないといけない
b[vs.back()] = vs[(sz - 2 + x) % (sz - 1)];
}
for (auto e : b)cout << e + 1 << " ";
cout << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t; cin >> t;
while (t--)solve();
return 0;
}
kwm_t