結果

問題 No.2496 LCM between Permutations
ユーザー shobonvipshobonvip
提出日時 2023-10-06 22:54:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,601 bytes
コンパイル時間 2,908 ms
コンパイル使用メモリ 222,496 KB
実行使用メモリ 25,592 KB
平均クエリ数 951.10
最終ジャッジ日時 2024-07-26 16:50:07
合計ジャッジ時間 6,961 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
25,196 KB
testcase_01 AC 19 ms
25,220 KB
testcase_02 AC 20 ms
25,220 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 22 ms
24,580 KB
testcase_06 AC 23 ms
24,964 KB
testcase_07 RE -
testcase_08 AC 20 ms
24,964 KB
testcase_09 RE -
testcase_10 AC 22 ms
25,220 KB
testcase_11 AC 22 ms
24,964 KB
testcase_12 AC 22 ms
25,124 KB
testcase_13 AC 22 ms
25,220 KB
testcase_14 AC 21 ms
24,580 KB
testcase_15 AC 27 ms
25,196 KB
testcase_16 AC 27 ms
25,324 KB
testcase_17 AC 28 ms
25,196 KB
testcase_18 AC 84 ms
24,580 KB
testcase_19 AC 67 ms
25,220 KB
testcase_20 AC 104 ms
25,348 KB
testcase_21 AC 83 ms
24,580 KB
testcase_22 AC 87 ms
24,836 KB
testcase_23 AC 123 ms
24,836 KB
testcase_24 AC 98 ms
25,220 KB
testcase_25 AC 126 ms
25,348 KB
testcase_26 AC 113 ms
24,556 KB
testcase_27 AC 111 ms
25,424 KB
testcase_28 AC 113 ms
24,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

struct Queries{
	int n;
	vector<vector<bool>> checked;
	vector<vector<int>> val;
	int cnt = 0;
	
	Queries(int _n) : n(_n) {
		checked.resize(n);
		val.resize(n);
		for (int i=0; i<n; i++){
			checked[i].resize(n);
			val[i].resize(n);
		}
	}

	int question(int i, int j){
		if (checked[i][j]){
			return val[i][j];
		}
		assert(cnt < 3*n);
		cout << "? " << i+1 << ' ' << j+1 << endl;
		int x; cin >> x;
		val[i][j] = x;
		checked[i][j] = true;
		cnt++;
		return val[i][j];
	}

	void answer(vector<int> a, vector<int> b){
		cout << "! ";
		for (int i=0; i<n; i++){
			cout << a[i] << ' ';
		}
		for (int i=0; i<n; i++){
			cout << b[i];
			if (i == n-1) cout << endl;
			else cout << ' ';
		}
	}

};

int main(){
	int n; cin >> n;
	if (n == 1){
		cout << "! 1 1" << endl;
		return 0;
	}

	vector<bool> p(n+1, true);
	for (int i=2; i<=n; i++){
		if (!p[i]) continue;
		for (int j=i*2; j<=n; j+=i){
			p[j] = false;
		}
	}

	int P = -1;
	for (int i=n; i>=2; i--){
		if (p[i]){
			P = i;
			break;
		}
	}	

	assert(P >= 2);

	// STEP 1 ... find max sosuu P less than or equal N either on A or B
	// if gcd(A[i], B) = P, then A[i] = P
	// if gcd(A[i], B[j]) = P * ??, then B[i] = P
	//
	// STEP 2 ...
	// WLOG assume A[i] = P
	// check all lcm(A[i], B[j])
	// if (A[i], B[j]) = A[i] = P, then B[j] = 1 or P.
	// check  lcm(A[k], B[j])
	// 
	// STEP 3 ...
	// you have B[j] = P, so check all.

	Queries que(n);
	int now = 0;

	vector<int> a(n,-1), b(n,-1);

	// N
	for (int i=0; i<n; i++){
		int x = que.question(0, i);
		now = __gcd(now, x);
	}

	vector<int> kouho;
	char wherep;

	if (now == P){
		wherep = 'A';
		for (int i=0; i<n; i++){
			if (que.question(0, i) == P){
				kouho.push_back(i);
			}else{
				b[i] = que.question(0, i) / P;
			}
		}
	}else{
		wherep = 'B';
		int targ = -1;
		for (int i=0; i<n; i++){
			if (que.question(0, i) % P == 0){
				b[i] = P;
				assert(targ == -1);
				targ = i;
			}
		}
		assert(targ >= 0);
		for (int i=0; i<n; i++){
			que.question(i, targ);
		}
		// N-1
		for (int i=0; i<n; i++){
			if (que.question(i, targ) == P){
				kouho.push_back(i);
			}else{
				a[i] = que.question(i, targ) / P;
			}
		}
	}

	for (int i=0; i<(int)kouho.size(); i++){
		if (wherep == 'A'){
			// 1
			if (que.question(1, kouho[i]) % P == 0){
				b[kouho[i]] = P;
				b[kouho[i^1]] = 1;
			}else{
				b[kouho[i]] = 1;
				b[kouho[i^1]] = P;
			}
			break;
		}else{
			int targ = -1;
			for (int j=0; j<n; j++){
				if (b[j] != P){
					targ = j;
					break;
				}
			}
			assert(targ >= 0);
			// 1
			if (que.question(kouho[i], targ) % P == 0){
				a[kouho[i]] = P;
				a[kouho[i^1]] = 1;
			}else{
				a[kouho[i]] = 1;
				a[kouho[i^1]] = P;
			}
			break;
		}
	}

	{
		int targ = -1;
		for (int i=0; i<n; i++){
			if (a[i] == P){
				targ = i;
				break;
			}
		}
		assert(targ >= 0);
		for (int i=0; i<n; i++){
			int x = que.question(targ, i);
			if (x == P && b[i] == -1){
				b[i] = 1;
			}
			if (x != P && b[i] == -1){
				b[i] = x / P;
			}
		}
	}

	//que.answer(a, b);

	{
		int targ = -1;
		for (int i=0; i<n; i++){
			if (b[i] == P){
				targ = i;
				break;
			}
		}
		assert(targ >= 0);
		for (int i=0; i<n; i++){
			int x = que.question(i, targ);
			if (x == P && a[i] == -1){
				a[i] = 1;
			}
			if (x != P && a[i] == -1){
				a[i] = x / P;
			}
	}
	}

	//que.answer(a, b);

	set<int> as, bs;
	for (int i=0; i<n; i++){
		assert(a[i] >= 1);
		as.insert(a[i]);
	}

	for (int i=0; i<n; i++){
		assert(b[i] >= 1);
		bs.insert(b[i]);
	}

	assert((int)as.size() == n);
	assert((int)bs.size() == n);
	
	que.answer(a, b);
}
0