結果
問題 | No.2496 LCM between Permutations |
ユーザー |
![]() |
提出日時 | 2023-10-06 23:27:14 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 98 ms / 2,000 ms |
コード長 | 2,999 bytes |
コンパイル時間 | 3,265 ms |
コンパイル使用メモリ | 278,700 KB |
実行使用メモリ | 28,356 KB |
平均クエリ数 | 720.28 |
最終ジャッジ日時 | 2024-07-26 17:08:03 |
合計ジャッジ時間 | 6,156 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 28 |
ソースコード
#include<bits/stdc++.h>namespace {#pragma GCC diagnostic ignored "-Wunused-function"#include<atcoder/all>#pragma GCC diagnostic warning "-Wunused-function"using namespace std;using namespace atcoder;#define rep(i,n) for(int i = 0; i < (int)(n); i++)#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)#define all(x) begin(x), end(x)#define rall(x) rbegin(x), rend(x)template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }using ll = long long;using P = pair<int,int>;using VI = vector<int>;using VVI = vector<VI>;using VL = vector<ll>;using VVL = vector<VL>;pair<vector<int>, vector<int>> primes_lpf(const int n) {vector<int> primes; primes.reserve(n / 10);vector<int> lpf(n + 1);for (int i = 2; i <= n; i += 2) lpf[i] = 2;for (int i = 3; i <= n; i += 6) lpf[i] = 3;if (2 <= n) primes.push_back(2);if (3 <= n) primes.push_back(3);// 5 * x <= n, x <= floor(n / 5)const int n5 = n / 5;int x = 5;char add_next = 2;for (; x <= n5; x += add_next, add_next ^= 2 ^ 4) {int px = lpf[x];if (px == 0) {lpf[x] = px = x;primes.push_back(x);}for (int i = 2;; ++i) {int q = primes[i];int y = q * x;if (y > n) break;lpf[y] = q;if (q == px) break;}}for (; x <= n; x += add_next, add_next ^= 2 ^ 4) {if (lpf[x] == 0) {lpf[x] = x;primes.push_back(x);}}return {move(primes), move(lpf)};}constexpr int PSIZE = 1000000;auto [primes, lpf] = primes_lpf(PSIZE);int memo[1010][1010];} int main() {ios::sync_with_stdio(false);cin.tie(0);memset(memo, -1, sizeof(memo));int n;cin >> n;bool flip = false;auto ask = [&](int i, int j) {if (flip) swap(i, j);if (memo[i][j] != -1) return memo[i][j];cout << "? " << i + 1 << ' ' << j + 1 << endl;int res;cin >> res;return memo[i][j] = res;};auto answer = [&](VI a, VI b) {if (flip) swap(a, b);cout << "!";for (int x : a) cout << ' ' << x;for (int x : b) cout << ' ' << x;cout << endl;};if (n == 1) {answer({1}, {1});return 0;}int pmx = *prev(upper_bound(all(primes), n));int ip = -1, jp = -1;rep(i, n) {if (i == n - 1) {ip = jp = n - 1;break;}int r = ask(i, i);if (r % pmx == 0) {if (ask(i, i + 1) % pmx == 0) {ip = i;} else {jp = i;}break;}}flip = ip == -1;if (flip) swap(ip, jp);VI a(n), b(n), oneb;rep(j, n) {b[j] = ask(ip, j) / pmx;if (b[j] == 1) oneb.emplace_back(j);}assert(oneb.size() == 2);int i_test = ip == 0 ? 1 : ip - 1;int j1;if (ask(i_test, oneb[0]) % pmx == 0) {b[oneb[0]] = pmx;j1 = oneb[1];} else {b[oneb[1]] = pmx;j1 = oneb[0];}rep(i, n) a[i] = ask(i, j1);answer(a, b);}