結果

問題 No.2496 LCM between Permutations
ユーザー shobonvipshobonvip
提出日時 2023-10-06 23:12:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 3,642 bytes
コンパイル時間 2,743 ms
コンパイル使用メモリ 221,524 KB
実行使用メモリ 24,396 KB
平均クエリ数 951.69
最終ジャッジ日時 2023-10-06 23:12:39
合計ジャッジ時間 6,454 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
23,556 KB
testcase_01 AC 26 ms
24,348 KB
testcase_02 AC 26 ms
23,424 KB
testcase_03 AC 30 ms
24,060 KB
testcase_04 AC 23 ms
23,400 KB
testcase_05 AC 22 ms
24,276 KB
testcase_06 AC 27 ms
23,400 KB
testcase_07 AC 27 ms
23,448 KB
testcase_08 AC 27 ms
23,412 KB
testcase_09 AC 22 ms
23,652 KB
testcase_10 AC 25 ms
24,396 KB
testcase_11 AC 31 ms
23,664 KB
testcase_12 AC 27 ms
24,276 KB
testcase_13 AC 24 ms
23,376 KB
testcase_14 AC 27 ms
23,844 KB
testcase_15 AC 33 ms
23,568 KB
testcase_16 AC 35 ms
24,264 KB
testcase_17 AC 36 ms
23,532 KB
testcase_18 AC 90 ms
23,856 KB
testcase_19 AC 78 ms
24,036 KB
testcase_20 AC 102 ms
23,856 KB
testcase_21 AC 91 ms
23,652 KB
testcase_22 AC 82 ms
23,664 KB
testcase_23 AC 115 ms
23,640 KB
testcase_24 AC 94 ms
23,676 KB
testcase_25 AC 126 ms
23,844 KB
testcase_26 AC 125 ms
24,276 KB
testcase_27 AC 125 ms
23,424 KB
testcase_28 AC 125 ms
23,600 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];
		}
		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);
	//cout << P << endl;

	// 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';
		a[0] = P;
		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;
			}
		}
	}

	assert(kouho.size() == 2);
	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