結果
問題 | No.2496 LCM between Permutations |
ユーザー | shobonvip |
提出日時 | 2023-10-06 23:11:39 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,642 bytes |
コンパイル時間 | 2,495 ms |
コンパイル使用メモリ | 219,544 KB |
実行使用メモリ | 25,580 KB |
平均クエリ数 | 634.59 |
最終ジャッジ日時 | 2024-07-26 16:56:43 |
合計ジャッジ時間 | 7,764 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | AC | 23 ms
24,580 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
ソースコード
#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'; 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; } } return 0; { 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); }