結果

問題 No.2519 Coins in Array
ユーザー nono00nono00
提出日時 2023-10-28 08:04:32
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 533 ms / 2,000 ms
コード長 3,630 bytes
コンパイル時間 3,257 ms
コンパイル使用メモリ 262,724 KB
実行使用メモリ 12,732 KB
最終ジャッジ日時 2023-10-28 08:04:46
合計ジャッジ時間 12,561 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,348 KB
testcase_01 AC 5 ms
8,348 KB
testcase_02 AC 5 ms
8,352 KB
testcase_03 AC 5 ms
8,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 5 ms
8,352 KB
testcase_07 AC 5 ms
8,348 KB
testcase_08 AC 5 ms
8,348 KB
testcase_09 AC 5 ms
8,348 KB
testcase_10 AC 5 ms
8,348 KB
testcase_11 AC 5 ms
8,352 KB
testcase_12 AC 6 ms
8,352 KB
testcase_13 AC 5 ms
8,352 KB
testcase_14 AC 5 ms
8,348 KB
testcase_15 AC 5 ms
8,352 KB
testcase_16 AC 5 ms
8,348 KB
testcase_17 AC 5 ms
8,348 KB
testcase_18 AC 5 ms
8,348 KB
testcase_19 AC 5 ms
8,352 KB
testcase_20 AC 5 ms
8,348 KB
testcase_21 AC 5 ms
8,348 KB
testcase_22 AC 6 ms
8,348 KB
testcase_23 AC 318 ms
11,428 KB
testcase_24 AC 533 ms
11,108 KB
testcase_25 AC 5 ms
8,352 KB
testcase_26 AC 5 ms
8,352 KB
testcase_27 AC 398 ms
12,700 KB
testcase_28 AC 398 ms
12,732 KB
testcase_29 AC 396 ms
12,720 KB
testcase_30 AC 356 ms
11,960 KB
testcase_31 AC 63 ms
8,912 KB
testcase_32 AC 137 ms
4,348 KB
testcase_33 AC 203 ms
4,348 KB
testcase_34 AC 133 ms
9,788 KB
testcase_35 AC 179 ms
4,348 KB
testcase_36 AC 103 ms
9,416 KB
testcase_37 AC 50 ms
8,812 KB
testcase_38 AC 149 ms
4,348 KB
testcase_39 AC 86 ms
4,348 KB
testcase_40 AC 288 ms
10,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

namespace nono {

struct Init {};

void solve([[maybe_unused]] const Init& init) {
    int n;
    std::cin >> n;
    std::vector<long long> a(n);
    for (int i = 0; i < n; i++) std::cin >> a[i];

    auto goodbye = [&](auto self, int n) -> void {
        if (n == 1) return;
        std::cout << n << ' ' << n - 1 << std::endl;
        self(self, n - 1);
    };

    auto output = [&](int i, int j) -> void {
        std::cout << 0 << std::endl;
        std::cout << i + 1 << ' ' << j + 1 << std::endl;
        goodbye(goodbye, n - 1);
    };

    if (std::ranges::find(a, 1) != a.end()) {
        int i = std::ranges::find(a, 1) - a.begin();
        int j = (i + 1) % n;
        output(i, j);
        return;
    }

    if (std::ranges::find(a, 0) != a.end()) {
        int i = std::ranges::find(a, 0) - a.begin();
        int j = (i + 1) % n;
        output(i, j);
        return;
    }

    constexpr int N = 200001;
    std::array<std::vector<int>, N> index;
    index.fill(std::vector<int>());

    for (int i = 0; i < n; i++) {
        int value = a[i];
        for (int j = 2; j * j <= value; j++) {
            if (value % j == 0) {
                index[j].push_back(i);
                while (value % j == 0) {
                    value /= j;
                }
            }
        }
        if (value != 1) {
            index[value].push_back(i);
        }
    }

    for (int i = 0; i < N; i++) {
        if (index[i].size() >= 2) {
            output(index[i][0], index[i][1]);
            return;
        }
    }

    if (n == 2) {
        long long x = a[0];
        long long y = a[1];
        std::cout << (x - 1) * (y - 1) << std::endl;
        goodbye(goodbye, 2);
    } else if (n == 3) {
        long long ans = 1e18;
        int ans_i = -1;
        for (int i = 0; i < 3; i++) {
            int j = (i + 1) % n;
            int k = (i - 1 + n) % n;
            long long v = (a[j] - 1) * (a[k] - 1);
            if (std::gcd(v, a[i]) != 1) {
                ans = 0;
                ans_i = i;
                break;
            } else if (ans > (v - 1) * (a[i] - 1)) {
                ans = (v - 1) * (a[i] - 1);
                ans_i = i;
            }
        }
        int j = (ans_i + 1) % n;
        int k = (ans_i - 1 + n) % n;

        std::cout << ans << std::endl;
        std::cout << j + 1 << ' ' << k + 1 << std::endl;
        goodbye(goodbye, 2);
    } else {
        std::cout << 0 << std::endl;
        auto it = std::ranges::find(a, 2);
        if (it == a.end() || (it - a.begin() >= 4)) {
            std::cout << 1 << ' ' << 2 << std::endl;
            std::cout << 1 << ' ' << 2 << std::endl;
            goodbye(goodbye, n - 2);
        } else {
            int i = (it - a.begin());
            if (i == 0) {
                std::cout << 2 << ' ' << 3 << std::endl;
                std::cout << 1 << ' ' << n - 1 << std::endl;
            } else if (i == 1) {
                std::cout << 1 << ' ' << 3 << std::endl;
                std::cout << 1 << ' ' << n - 1 << std::endl;
            } else if (i == 2) {
                std::cout << 1 << ' ' << 2 << std::endl;
                std::cout << 1 << ' ' << n - 1 << std::endl;
            } else if (i == 3) {
                std::cout << 1 << ' ' << 2 << std::endl;
                std::cout << 2 << ' ' << n - 1 << std::endl;
            }
            goodbye(goodbye, n - 2);
        }
    }
}

}  //  namespace nono

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    std::cout << std::fixed << std::setprecision(16);

    int t = 1;
    nono::Init init;

    while (t--) nono::solve(init);
}
0