結果

問題 No.2496 LCM between Permutations
ユーザー rniya
提出日時 2023-10-06 23:03:19
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 4,151 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 202,948 KB
最終ジャッジ日時 2025-02-17 05:30:25
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif
using namespace std;
typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
template <class T> istream& operator>>(istream& is, vector<T>& v) {
for (auto& x : v) is >> x;
return is;
}
template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
auto sep = "";
for (const auto& x : v) os << exchange(sep, " ") << x;
return os;
}
template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }
template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }
template <class T> void mkuni(vector<T>& v) {
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
}
template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }
/*
1 N p
lcm(p, x) x = 1, p p px
*/
int N;
vector<int> A, B;
int query(int i, int j) {
assert(0 <= i and i < N);
assert(0 <= j and j < N);
cout << "? " << i + 1 << ' ' << j + 1 << endl;
int res;
#ifdef LOCAL
res = lcm(A[i], B[j]);
#else
cin >> res;
#endif
return res;
}
void answer(vector<int> a, vector<int> b) {
cout << '!';
for (int& x : a) cout << ' ' << x;
for (int& x : b) cout << ' ' << x;
cout << endl;
#ifdef LOCAL
assert(a == A and b == B);
#endif
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
#ifdef LOCAL
A.resize(N);
B.resize(N);
iota(all(A), 1);
iota(all(B), 1);
random_device seed_gen;
mt19937 engine(seed_gen());
shuffle(all(A), engine);
shuffle(all(B), engine);
debug(A, B);
#endif
vector<int> A(N, -1), B(N, -1);
if (N == 1) {
A[0] = B[0] = 1;
answer(A, B);
return 0;
}
vector<bool> is_prime(N + 1, true);
int p = -1;
for (int i = 2; i <= N; i++) {
if (not is_prime[i]) continue;
p = i;
for (int j = i + i; j <= N; j += i) is_prime[j] = false;
}
vector<int> C(N);
for (int i = 0; i < N; i++) C[i] = query(0, i);
A[0] = *min_element(all(C));
debug(C, A[0]);
int pos = -1;
if (A[0] == p) {
pos = 0;
vector<int> cand;
for (int i = 0; i < N; i++) {
if (C[i] == p)
cand.emplace_back(i);
else
B[i] = C[i] / p;
}
assert(int(cand.size()) == 2);
vector<int> D(N);
for (int i = 0; i < N; i++) D[i] = query(i, cand[0]);
B[cand[0]] = *min_element(all(D));
B[cand[1]] = 1 + p - B[cand[0]];
int one = -1;
for (int i = 0; i < 2; i++) {
if (B[cand[i]] == 1) {
one = cand[i];
}
}
assert(one != -1);
debug(one);
for (int i = 1; i < N; i++) A[i] = query(i, one);
} else {
for (int i = 0; i < N; i++) {
if (C[i] == A[0] * p) {
assert(pos == -1);
pos = i;
}
}
assert(pos != -1);
B[pos] = p;
vector<int> D(N), cand;
for (int i = 0; i < N; i++) {
D[i] = query(i, pos);
if (D[i] == p)
cand.emplace_back(i);
else
A[i] = D[i] / p;
}
assert(int(cand.size()) == 2);
if (query(cand[0], (pos + 1) % N) % p == 0)
A[cand[0]] = p;
else
A[cand[0]] = 1;
A[cand[1]] = 1 + p - A[cand[0]];
debug(cand, A);
int one = -1;
for (int i = 0; i < 2; i++) {
if (A[cand[i]] == 1) {
one = cand[i];
}
}
assert(one != -1);
debug(one);
for (int i = 0; i < N; i++) {
if (B[i] != -1) continue;
B[i] = query(one, i);
}
}
answer(A, B);
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0