結果

問題 No.2732 Similar Permutations
ユーザー shobonvipshobonvip
提出日時 2024-04-19 23:19:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 3,520 bytes
コンパイル時間 5,500 ms
コンパイル使用メモリ 269,044 KB
実行使用メモリ 15,616 KB
最終ジャッジ日時 2024-04-19 23:20:21
合計ジャッジ時間 21,098 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
15,488 KB
testcase_01 AC 7 ms
15,616 KB
testcase_02 AC 30 ms
6,784 KB
testcase_03 AC 54 ms
7,552 KB
testcase_04 AC 19 ms
6,016 KB
testcase_05 AC 15 ms
5,888 KB
testcase_06 AC 8 ms
5,504 KB
testcase_07 AC 42 ms
6,928 KB
testcase_08 AC 21 ms
6,272 KB
testcase_09 AC 12 ms
5,632 KB
testcase_10 AC 105 ms
9,216 KB
testcase_11 AC 9 ms
5,632 KB
testcase_12 AC 95 ms
9,244 KB
testcase_13 AC 111 ms
9,236 KB
testcase_14 AC 94 ms
9,244 KB
testcase_15 AC 96 ms
9,244 KB
testcase_16 AC 71 ms
8,104 KB
testcase_17 AC 66 ms
8,064 KB
testcase_18 AC 87 ms
8,868 KB
testcase_19 AC 103 ms
9,376 KB
testcase_20 AC 95 ms
9,216 KB
testcase_21 AC 95 ms
9,496 KB
testcase_22 AC 58 ms
7,680 KB
testcase_23 AC 94 ms
9,240 KB
testcase_24 AC 55 ms
7,552 KB
testcase_25 AC 95 ms
9,240 KB
testcase_26 AC 78 ms
8,576 KB
testcase_27 AC 97 ms
9,236 KB
testcase_28 AC 74 ms
8,448 KB
testcase_29 AC 92 ms
9,244 KB
testcase_30 AC 89 ms
8,064 KB
testcase_31 AC 92 ms
9,240 KB
testcase_32 AC 87 ms
8,996 KB
testcase_33 AC 97 ms
9,240 KB
testcase_34 AC 61 ms
7,808 KB
testcase_35 AC 92 ms
9,240 KB
testcase_36 AC 86 ms
8,868 KB
testcase_37 AC 92 ms
9,364 KB
testcase_38 AC 81 ms
8,704 KB
testcase_39 AC 98 ms
9,240 KB
testcase_40 AC 79 ms
8,712 KB
testcase_41 AC 99 ms
9,364 KB
testcase_42 AC 71 ms
8,192 KB
testcase_43 AC 92 ms
9,244 KB
testcase_44 AC 51 ms
7,352 KB
testcase_45 AC 92 ms
9,232 KB
testcase_46 AC 84 ms
8,960 KB
testcase_47 AC 91 ms
9,232 KB
testcase_48 AC 56 ms
7,680 KB
testcase_49 AC 87 ms
9,236 KB
testcase_50 AC 54 ms
7,680 KB
testcase_51 AC 81 ms
9,364 KB
testcase_52 AC 71 ms
8,644 KB
testcase_53 AC 98 ms
9,376 KB
testcase_54 AC 96 ms
9,248 KB
testcase_55 AC 92 ms
9,240 KB
testcase_56 AC 94 ms
9,268 KB
testcase_57 AC 47 ms
7,372 KB
testcase_58 AC 65 ms
8,192 KB
testcase_59 AC 62 ms
8,064 KB
testcase_60 AC 53 ms
7,608 KB
testcase_61 AC 48 ms
7,300 KB
testcase_62 AC 78 ms
8,712 KB
testcase_63 AC 7 ms
15,488 KB
testcase_64 AC 7 ms
15,616 KB
testcase_65 AC 7 ms
15,616 KB
testcase_66 AC 6 ms
15,580 KB
testcase_67 AC 7 ms
15,360 KB
testcase_68 AC 7 ms
15,488 KB
testcase_69 AC 7 ms
15,488 KB
testcase_70 AC 7 ms
15,480 KB
testcase_71 AC 7 ms
15,488 KB
testcase_72 AC 8 ms
15,564 KB
testcase_73 AC 7 ms
15,472 KB
testcase_74 AC 7 ms
15,488 KB
testcase_75 AC 7 ms
15,472 KB
testcase_76 AC 6 ms
15,556 KB
testcase_77 AC 7 ms
15,488 KB
testcase_78 AC 6 ms
15,504 KB
testcase_79 AC 7 ms
15,552 KB
testcase_80 AC 13 ms
15,560 KB
testcase_81 AC 8 ms
15,488 KB
testcase_82 AC 6 ms
15,616 KB
testcase_83 AC 7 ms
15,472 KB
testcase_84 AC 7 ms
15,488 KB
testcase_85 AC 6 ms
15,488 KB
testcase_86 AC 7 ms
15,488 KB
testcase_87 AC 6 ms
15,488 KB
testcase_88 AC 8 ms
15,616 KB
testcase_89 AC 7 ms
15,360 KB
testcase_90 AC 7 ms
15,616 KB
testcase_91 AC 7 ms
15,592 KB
testcase_92 AC 7 ms
15,556 KB
testcase_93 AC 8 ms
15,488 KB
testcase_94 AC 7 ms
15,472 KB
testcase_95 AC 7 ms
15,616 KB
testcase_96 AC 7 ms
15,468 KB
testcase_97 AC 8 ms
15,616 KB
testcase_98 AC 7 ms
15,488 KB
testcase_99 AC 12 ms
15,488 KB
testcase_100 AC 7 ms
15,488 KB
testcase_101 AC 6 ms
15,496 KB
testcase_102 AC 6 ms
15,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

0