結果

問題 No.2519 Coins in Array
ユーザー milanis48663220milanis48663220
提出日時 2023-10-27 22:17:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 3,741 bytes
コンパイル時間 1,177 ms
コンパイル使用メモリ 123,032 KB
実行使用メモリ 5,296 KB
最終ジャッジ日時 2023-10-27 22:17:44
合計ジャッジ時間 7,395 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 284 ms
4,988 KB
testcase_24 AC 284 ms
4,724 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 3 ms
4,348 KB
testcase_27 AC 285 ms
4,988 KB
testcase_28 AC 291 ms
4,988 KB
testcase_29 AC 288 ms
4,988 KB
testcase_30 AC 261 ms
5,296 KB
testcase_31 AC 48 ms
4,592 KB
testcase_32 AC 142 ms
4,708 KB
testcase_33 AC 203 ms
5,136 KB
testcase_34 AC 97 ms
4,584 KB
testcase_35 AC 185 ms
4,828 KB
testcase_36 AC 77 ms
4,468 KB
testcase_37 AC 36 ms
4,548 KB
testcase_38 AC 153 ms
4,744 KB
testcase_39 AC 90 ms
4,500 KB
testcase_40 AC 278 ms
4,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

const ll inf = 1e18;

ll f(ll p, ll q){
    if(gcd(p, q) != 1) return 0;
    return (p-1)*(q-1);
}

const int N = 200000;

int cnt[N+5];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        cnt[a[i]]++;
    }
    for(int x = 2; x <= N; x++){
        int c = 0;
        for(int i = 1; i*x <= N; i++){
            c += cnt[i*x];
        }
        if(c >= 2){
            vector<int> v;
            for(int i = 0; i < n; i++){
                if(a[i]%x == 0) v.push_back(i);
            }
           
            cout << 0 << endl;
            cout << v[0]+1 << ' ' << v[1]+1 << endl;
            for(int i = 0; i < n-2; i++) cout << 1 << ' ' << 2 << endl;
            return 0;
        }
    }
    vector<int> odd_indices;
    for(int i = 0; i < n; i++){
        if(a[i]%2 == 1) odd_indices.push_back(i);
    }
    if(odd_indices.size() >= 4){
        int m = odd_indices.size();
        cout << 0 << endl;
        cout << odd_indices[m-2]+1 << ' ' << odd_indices[m-1]+1 << endl;
        cout << odd_indices[m-4]+1 << ' ' << odd_indices[m-3]+1 << endl;
        cout << n-3 << ' ' << n-2 << endl;
        // n-3個
        for(int i = 0; i < n-4; i++) cout << 1 << ' ' << 2 << endl;
        return 0;
    }

    if(n == 4){
        // 一つだけ偶数があるはず
        int m = odd_indices.size();
        assert(m == 3);
        cout << 0 << endl;
        cout << odd_indices[m-2]+1 << ' ' << odd_indices[m-1]+1 << endl;
        vector<int> rem;
        for(int i = 0; i < n; i++){
            if(i == odd_indices[m-2] || i == odd_indices[m-1]) continue;
            rem.push_back(i);
        }
        for(int i = 0; i < rem.size(); i++){
            if(a[rem[i]]%2 == 0) cout << i+1 << ' ' << 3 << endl;
        }
        cout << 1 << ' ' << 2 << endl;
        return 0;
    }

    if(n == 3){
        ll ans = 4e18;
        int exepted = -1;
        if(chmin(ans, f(a[0], f(a[1], a[2])))) exepted = 0;
        if(chmin(ans, f(a[1], f(a[2], a[0])))) exepted = 1;
        if(chmin(ans, f(a[2], f(a[0], a[1])))) exepted = 2;
        cout << ans << endl;
        if(exepted == 0) cout << 2 << ' ' << 3 << endl;
        if(exepted == 1) cout << 3 << ' ' << 1 << endl;
        if(exepted == 2) cout << 1 << ' ' << 2 << endl;
        cout << 1 << ' ' << 2 << endl;
        return 0;
    }

    if(n == 2){
        cout << f(a[0], a[1]) << endl;
        cout << 1 << ' ' << 2 << endl;
        return 0;
    }
}
0