結果

問題 No.2519 Coins in Array
ユーザー eve__fuyuki
提出日時 2023-11-29 05:15:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,294 bytes
コンパイル時間 2,056 ms
コンパイル使用メモリ 198,660 KB
最終ジャッジ日時 2025-02-18 02:21:44
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
void fast_io() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}
long long f(long long x, long long y) {
    if (gcd(x, y) == 1) {
        return (x - 1) * (y - 1);
    }
    return 0;
}
int main() {
    fast_io();
    int n;
    cin >> n;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    if (n == 2) {
        cout << f(a[0], a[1]) << endl;
        cout << "1 2" << endl;
        return 0;
    }
    if (n == 3) {
        vector<long long> ff{f(a[0], f(a[1], a[2])), f(a[1], f(a[0], a[2])),
                             f(a[2], f(a[0], a[1]))};
        int idx = min_element(ff.begin(), ff.end()) - ff.begin();
        cout << ff[idx] << endl;
        if (idx == 0) {
            cout << "2 3" << endl;
        } else if (idx == 1) {
            cout << "1 3" << endl;
        } else {
            cout << "1 2" << endl;
        }
        cout << "1 2" << endl;
        return 0;
    }
    for (int i = 0; i < n - 1; i++) {
        long long ai = a.back();
        a.pop_back();
        long long aj = a.back();
        a.pop_back();
        a.push_back(f(ai, aj));
    }
    cout << a[0] << endl;
    for (int i = n - 1; i >= 1; i--) {
        cout << i << " " << i + 1 << endl;
    }
}
0