結果

問題 No.2519 Coins in Array
ユーザー ochiaigawaochiaigawa
提出日時 2023-10-28 00:29:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,915 bytes
コンパイル時間 2,700 ms
コンパイル使用メモリ 224,516 KB
実行使用メモリ 8,560 KB
最終ジャッジ日時 2023-10-28 00:29:31
合計ジャッジ時間 8,993 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 WA -
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 WA -
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 307 ms
6,696 KB
testcase_24 AC 250 ms
7,420 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 340 ms
8,552 KB
testcase_28 AC 313 ms
8,512 KB
testcase_29 AC 319 ms
8,560 KB
testcase_30 AC 276 ms
8,020 KB
testcase_31 AC 43 ms
4,836 KB
testcase_32 AC 131 ms
6,144 KB
testcase_33 AC 184 ms
7,032 KB
testcase_34 AC 87 ms
5,556 KB
testcase_35 AC 177 ms
6,768 KB
testcase_36 AC 82 ms
5,268 KB
testcase_37 AC 32 ms
4,620 KB
testcase_38 AC 139 ms
6,392 KB
testcase_39 AC 80 ms
5,448 KB
testcase_40 AC 33 ms
5,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long f(long long x, long long y) {
  if(__gcd(x, y) > 1 || x == 0 || y == 0) {
    return 0;
  } else {
    return (x - 1) * (y - 1);
  }
}
int main() {
  cin.tie(0); cout.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, vector<int>> mp;
  for(int i = 0; i < N; i++) {
    cin >> A[i];
    int B = A[i];
    for(int j = 2; j * j <= A[i]; j++) {
      if(B % j == 0) {
        while(B % j == 0) B /= j;
        if(mp.count(j) == 0) {
          mp[j] = vector<int>{i};
        } else {
          mp[j].emplace_back(i);
        }
      }
    }
    if(B > 1) {
      if(mp.count(B) == 0) {
        mp[B] = vector<int>{i};
      } else {
        mp[B].emplace_back(i);
      }
    }
  }
  for(auto [p, v] : mp) {
    if((int) v.size() > 1) {
      cout << 0 << '\n';
      cout << v[0] + 1 << ' ' << v[1] + 1 << '\n';
      for(int i = 1; i < N - 1; i++) {
        cout << 1 << ' ' << N - i << '\n';
      }
      return 0;
    }
  }
  priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
  atcoder::fenwick_tree<int> fw(N + N);
  for(int i = 0; i < N; i++) {
    pq.push(make_pair(A[i], i));
  }
  vector<pair<int, int>> ans;
  for(int t = 0; t < N - 1; t++) {
    pair<long long, int> p = pq.top();
    pq.pop();
    pair<long long, int> q = pq.top();
    pq.pop();
    long long x = p.first;
    long long y = q.first;
    int ix = p.second;
    int iy = q.second;
    long long z = f(x, y);
    ans.push_back(make_pair(ix - fw.sum(0, ix), iy - fw.sum(0, iy)));
    fw.add(ix, 1);
    fw.add(iy, 1);
    pq.push(make_pair(z, N + t));
  }
  cout << pq.top().first << '\n';
  for(pair<int, int> a : ans) {
    cout << a.first + 1 << ' ' << a.second + 1 << '\n';
  }
  return 0;
}
0