結果

問題 No.2519 Coins in Array
ユーザー gucci0512gucci0512
提出日時 2023-10-27 23:29:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 314 ms / 2,000 ms
コード長 4,029 bytes
コンパイル時間 4,188 ms
コンパイル使用メモリ 232,444 KB
実行使用メモリ 7,992 KB
最終ジャッジ日時 2023-10-27 23:29:47
合計ジャッジ時間 11,218 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,216 KB
testcase_01 AC 4 ms
5,216 KB
testcase_02 AC 4 ms
5,216 KB
testcase_03 AC 4 ms
5,216 KB
testcase_04 AC 4 ms
5,216 KB
testcase_05 AC 4 ms
5,216 KB
testcase_06 AC 4 ms
5,216 KB
testcase_07 AC 4 ms
5,216 KB
testcase_08 AC 4 ms
5,216 KB
testcase_09 AC 5 ms
5,216 KB
testcase_10 AC 4 ms
5,216 KB
testcase_11 AC 5 ms
5,216 KB
testcase_12 AC 5 ms
5,216 KB
testcase_13 AC 4 ms
5,216 KB
testcase_14 AC 5 ms
5,216 KB
testcase_15 AC 4 ms
5,216 KB
testcase_16 AC 5 ms
5,216 KB
testcase_17 AC 4 ms
5,216 KB
testcase_18 AC 4 ms
5,216 KB
testcase_19 AC 4 ms
5,216 KB
testcase_20 AC 4 ms
5,216 KB
testcase_21 AC 4 ms
5,216 KB
testcase_22 AC 4 ms
5,216 KB
testcase_23 AC 285 ms
7,992 KB
testcase_24 AC 286 ms
7,992 KB
testcase_25 AC 4 ms
5,216 KB
testcase_26 AC 4 ms
5,216 KB
testcase_27 AC 286 ms
7,992 KB
testcase_28 AC 285 ms
7,992 KB
testcase_29 AC 314 ms
7,992 KB
testcase_30 AC 260 ms
7,728 KB
testcase_31 AC 48 ms
5,352 KB
testcase_32 AC 149 ms
6,408 KB
testcase_33 AC 211 ms
6,936 KB
testcase_34 AC 98 ms
5,880 KB
testcase_35 AC 184 ms
6,936 KB
testcase_36 AC 77 ms
5,616 KB
testcase_37 AC 37 ms
5,220 KB
testcase_38 AC 154 ms
6,408 KB
testcase_39 AC 89 ms
5,880 KB
testcase_40 AC 278 ms
7,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
// url 
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=b-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x);
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double lld;
using namespace std;
using namespace atcoder;
const ll inf=(1ll<<60);
ll gcd(ll a,ll b){
    if(a==0||b==0)return a+b;
    ll r;
    r=a%b;
    if(r==0){
        return b;
    }
    else{
        return gcd(b,r);
    }
}
const int eratosSize=201010;
int eratos[eratosSize];
void Init(){
    rep(i,0,eratosSize)eratos[i]=-1;
    eratos[0]=1;
    eratos[1]=1;
    rep(i,2,eratosSize){
        if(eratos[i]!=-1)continue;
        eratos[i]=i;
        int x=i*2;
        while(x<eratosSize){
            if(eratos[x]==-1){
                eratos[x]=i;
            }
            x+=i;
        }
    }
}
int idx[eratosSize];    
typedef pair<ll,int> P;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // int x,y;cin >> x >> y;
    // rep(z,1,x*y+1){
    //     int a=0;
    //     cout << z << " ";
    //     while(a*x<=z){
    //         int b=z-a*x;
    //         if(b%y==0){
    //             cout << a << " " << b/y << endl;
    //             break;
    //         }
    //         a++;
    //     }
    //     if(a*x>z){
    //         cout << -1 << " " << -1 << endl;
    //     }
    // }
    Init();
    fill(idx,idx+eratosSize,-1);
    int N;cin >> N;
    vector<P> A(N);
    int a=-1,b=-1;
    int one=-1;
    rep(i,0,N){
        cin >> A[i].first;
        A[i].second=i;
        if(A[i].first<=1)one=i;
        if(a!=-1)continue;
        int tmp=A[i].first;
        while(tmp>1){
            int p=eratos[tmp];
            if(idx[p]==-1){
                idx[p]=i;
            }
            else if(idx[p]!=i){
                a=idx[p];
                b=i;
                break;
            }
            while(tmp%p==0){
                tmp/=p;
            }
        }
    }
    if(a!=-1){
        cout << 0 << endl;
        cout << a+1 << " " << b+1 << endl;
        rrep(i,1,N-1){
            cout << 1 << " " << i+1 << endl;
        }
    }
    else if(one!=-1){
        cout << 0 << endl;
        cout << one+1 << " " << 1 << endl;
        rrep(i,1,N-1){
            cout << 1 << " " << i+1 << endl;
        }
    }
    else if(N==2){
        ll M=(A[0].first-1)*(A[1].first-1);
        cout << M << endl;
        cout << 1 << " " << 2 << endl;
    }
    else if(idx[2]!=-1&&N>3){
        int x=-1,y=-1;
        rep(i,0,N){
            if(A[i].first==2)continue;
            if(x==-1){
                x=i;
            }
            else{
                y=i;
            }
        }
        cout << 0 << endl;
        cout << x+1 << " " << y+1 << endl;
        int z=idx[2];
        if(x<z&&y<z)z-=2;
        else if(x<z||y<z)z--;
        cout << z+1 << " " << N-1 << endl;
        rrep(i,1,N-2){
            cout << 1 << " " << i+1 << endl;
        }
    }
    else if(N>=4){
        cout << 0 << endl;
        cout << N-1 << " " << N << endl;
        cout << N-3 << " " << N-2 << endl;
        cout << N-3 << " " << N-2 << endl;
        rrep(i,1,N-3){
            cout << 1 << " " << i+1 << endl;
        }
    }
    else{
        ll M=inf;
        int x=-1,y=-1;
        rep(a,0,3)rep(b,a+1,3){
            ll tmp=(A[a].first-1)*(A[b].first-1);
            int c=3-a-b;
            ll g=gcd(tmp,A[c].first);
            if(g!=1){
                if(chmin(M,0ll)){
                    x=a;
                    y=b;
                }
            }
            else{
                tmp=(tmp-1)*(A[c].first-1);
                if(chmin(M,tmp)){
                    x=a;
                    y=b;
                }
            }
        }
        cout << M << endl;
        cout << x+1 << " " << y+1 << endl;
        cout << 1 << " " << 2 << endl;
    }
}
0