結果
問題 |
No.2732 Similar Permutations
|
ユーザー |
![]() |
提出日時 | 2024-04-19 23:19:55 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 113 ms / 2,000 ms |
コード長 | 3,520 bytes |
コンパイル時間 | 4,602 ms |
コンパイル使用メモリ | 267,012 KB |
最終ジャッジ日時 | 2025-02-21 05:36:35 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 101 |
ソースコード
#include<bits/stdc++.h> using namespace std; //* ATCODER #include<atcoder/all> using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include<boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template <typename T> T max(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template <typename T> T min(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template <typename T> T sum(vector<T> &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } int main(){ random_device seed_gen; mt19937 engine(seed_gen()); int n; cin >> n; vector<int> a(n); rep(i,0,n) cin >> a[i]; if (n <= 10){ vector<int> p(n); iota(p.begin(),p.end(),1); vector mem(1<<19,vector<int>(0)); do{ int r=0; rep(i,0,n)r^=a[i]+p[i]; if(mem[r].size()>0){ rep(i,0,n){ cout << mem[r][i] << ' '; } cout << '\n'; rep(i,0,n){ cout << p[i] << ' '; } cout << '\n'; return 0; } mem[r] = p; }while(next_permutation(p.begin(),p.end())); cout << -1 << '\n'; }else{ vector<int> mem(1<<19,-1); vector<int> p(n); iota(p.begin(), p.end(), 1); vector<mint> hash1(n); vector<mint> hash2(n); uniform_int_distribution<int> dist(0,998244353); rep(i,0,n){ hash1[i] = dist(engine); hash2[i] = dist(engine); } mint now1=0,now2=0; int val=0; rep(i,0,n){ now1 += hash1[i]*p[i]; now2 += hash2[i]*p[i]; val ^= p[i] + a[i]; } mem[val]=0; vector<pair<int,int>> sousa; uniform_int_distribution<int> dist2(0,n-1); set<pair<int,int>> st; int cnt = 0; while(true){ int u=dist2(engine); int v=dist2(engine); if(u==v)continue; now1 -= hash1[u]*p[u]; now2 -= hash2[u]*p[u]; now1 -= hash1[v]*p[v]; now2 -= hash2[v]*p[v]; now1 += hash1[u]*p[v]; now2 += hash2[u]*p[v]; now1 += hash1[v]*p[u]; now2 += hash2[v]*p[u]; val ^= p[u]+a[u]; val ^= p[v]+a[v]; val ^= p[u]+a[v]; val ^= p[v]+a[u]; sousa.push_back(pair(u,v)); swap(p[u],p[v]); if (st.find(pair(now1.val(),now2.val()))!=st.end()){ swap(p[u],p[v]); sousa.pop_back(); now1 += hash1[u]*p[u]; now2 += hash2[u]*p[u]; now1 += hash1[v]*p[v]; now2 += hash2[v]*p[v]; now1 -= hash1[u]*p[v]; now2 -= hash2[u]*p[v]; now1 -= hash1[v]*p[u]; now2 -= hash2[v]*p[u]; val ^= p[u]+a[u]; val ^= p[v]+a[v]; val ^= p[u]+a[v]; val ^= p[v]+a[u]; continue; } cnt++; st.insert(pair(now1.val(),now2.val())); if (mem[val]==-1){ mem[val]=cnt; }else{ { vector<int> q(n); iota(q.begin(),q.end(),1); rep(i,0,mem[val]){ auto[x,y] = sousa[i]; swap(q[x],q[y]); } rep(i,0,n){ cout << q[i] << ' '; } cout << '\n'; } { vector<int> q(n); iota(q.begin(),q.end(),1); rep(i,0,cnt){ auto[x,y] = sousa[i]; swap(q[x],q[y]); } rep(i,0,n){ cout << q[i] << ' '; } cout << '\n'; } return 0; } } } }