結果

問題 No.2519 Coins in Array
ユーザー 👑 emthrmemthrm
提出日時 2023-10-27 22:45:33
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,955 bytes
コンパイル時間 3,331 ms
コンパイル使用メモリ 259,284 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-09-25 14:45:57
合計ジャッジ時間 7,327 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 36 WA * 1
権限があれば一括ダウンロードができます

ソースコード

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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
ll f(const ll i, const ll j) {
return gcd(i, j) == 1 ? (i - 1) * (j - 1) : 0;
}
int main() {
constexpr int A = 200000;
int n; cin >> n;
vector<int> a(n); REP(i, n) cin >> a[i];
vector<pair<int, int>> ans;
const auto finish = [&]() -> void {
const int m = n - ans.size();
for (int i = m - 1; i >= 1; --i) {
ans.emplace_back(i - 1, i);
}
cout << 0 << '\n';
for (const auto& [i, j] : ans) cout << i + 1 << ' ' << j + 1 << '\n';
exit(0);
};
REP(i, n) {
if (a[i] == 0 || a[i] == 1) {
ans.emplace_back(i, (i + 1) % n);
finish();
}
}
array<int, A + 1> indices{};
fill(indices.begin(), indices.end(), -1);
REP(i, n) {
if (indices[a[i]] != -1) {
ans.emplace_back(indices[a[i]], i);
finish();
}
indices[a[i]] = i;
}
for (int d = 2; d <= A; ++d) {
int memo = -1;
for (int i = d; i <= A; i += d) {
if (memo == -1) {
memo = indices[i];
} else if (memo != -1 && indices[i] != -1) {
ans.emplace_back(memo, indices[i]);
finish();
}
}
}
vector<int> odd;
REP(i, n) {
if (a[i] % 2 == 1) odd.emplace_back(i);
}
if (odd.size() >= 4) {
odd.resize(4);
ans.emplace_back(odd.front(), odd[1]);
ans.emplace_back(odd.end()[-2], odd.back());
ans.emplace_back(n - 4, n - 3);
finish();
}
if (n == 2) {
cout << f(a.front(), a.back()) << '\n'
<< 1 << ' ' << 2 << '\n';
return 0;
}
if (n == 4) {
assert(odd.size() == 3);
ans.emplace_back(odd.front(), odd[1]);
a.erase(next(a.begin(), odd[1]));
a.erase(next(a.begin(), odd.front()));
a.emplace_back(f(a[odd.front()], a[odd[1]]));
REP(i, a.size()) FOR(j, i + 1, a.size()) {
if (a[i] % 2 == 0 && a[j] % 2 == 0) {
ans.emplace_back(i, j);
finish();
}
}
}
assert(n == 3);
ll v = LINF;
REP(i, n) FOR(j, i + 1, n) {
const int k = i ^ j ^ 0 ^ 1 ^ 2;
if (chmin(v, f(f(a[i], a[j]), a[k]))) ans = {{i, j}, {0, 1}};
}
cout << v << '\n';
for (const auto& [i, j] : ans) cout << i + 1 << ' ' << j + 1 << '\n';
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0