結果
問題 | No.2496 LCM between Permutations |
ユーザー |
![]() |
提出日時 | 2023-10-06 22:51:35 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,612 bytes |
コンパイル時間 | 2,691 ms |
コンパイル使用メモリ | 222,264 KB |
実行使用メモリ | 25,476 KB |
平均クエリ数 | 951.03 |
最終ジャッジ日時 | 2024-07-26 16:48:22 |
合計ジャッジ時間 | 7,131 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | AC | 22 ms
24,836 KB |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | AC | 23 ms
24,580 KB |
testcase_06 | AC | 23 ms
25,220 KB |
testcase_07 | RE | - |
testcase_08 | AC | 24 ms
24,580 KB |
testcase_09 | RE | - |
testcase_10 | AC | 24 ms
25,448 KB |
testcase_11 | AC | 24 ms
24,964 KB |
testcase_12 | AC | 24 ms
25,220 KB |
testcase_13 | AC | 23 ms
24,580 KB |
testcase_14 | AC | 23 ms
24,580 KB |
testcase_15 | AC | 30 ms
25,196 KB |
testcase_16 | AC | 31 ms
25,196 KB |
testcase_17 | AC | 34 ms
24,940 KB |
testcase_18 | AC | 94 ms
24,580 KB |
testcase_19 | AC | 70 ms
24,964 KB |
testcase_20 | AC | 96 ms
24,836 KB |
testcase_21 | AC | 83 ms
24,580 KB |
testcase_22 | AC | 78 ms
24,820 KB |
testcase_23 | AC | 107 ms
25,220 KB |
testcase_24 | AC | 92 ms
24,836 KB |
testcase_25 | AC | 115 ms
24,836 KB |
testcase_26 | AC | 117 ms
24,556 KB |
testcase_27 | AC | 117 ms
24,812 KB |
testcase_28 | AC | 116 ms
25,196 KB |
ソースコード
#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); 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); } 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; } } } //que.answer(a, b); for (int i=0; i<(int)kouho.size(); i++){ if (wherep == 'A'){ 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); 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; } } //que.answer(a, b); { int targ = -1; for (int i=0; i<n; i++){ if (a[i] == P){ targ = i; break; } } assert(targ >= 1); 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 >= 1); 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); }