結果

問題 No.2519 Coins in Array
ユーザー 👑 emthrmemthrm
提出日時 2023-10-27 22:50:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,941 bytes
コンパイル時間 4,492 ms
コンパイル使用メモリ 257,976 KB
実行使用メモリ 6,864 KB
最終ジャッジ日時 2023-10-27 22:50:46
合計ジャッジ時間 8,247 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 45 ms
6,864 KB
testcase_24 AC 43 ms
6,864 KB
testcase_25 AC 5 ms
4,380 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 45 ms
6,864 KB
testcase_28 AC 45 ms
6,864 KB
testcase_29 AC 46 ms
6,864 KB
testcase_30 AC 41 ms
6,788 KB
testcase_31 AC 9 ms
4,880 KB
testcase_32 AC 22 ms
4,876 KB
testcase_33 AC 33 ms
5,856 KB
testcase_34 AC 17 ms
5,280 KB
testcase_35 AC 29 ms
5,236 KB
testcase_36 AC 13 ms
5,124 KB
testcase_37 AC 7 ms
4,788 KB
testcase_38 AC 25 ms
4,984 KB
testcase_39 AC 15 ms
4,444 KB
testcase_40 AC 38 ms
6,864 KB
権限があれば一括ダウンロードができます

ソースコード

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 (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.end()[-2], odd.back());
    ans.emplace_back(odd.front(), odd[1]);
    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;
}
0