結果

問題 No.2519 Coins in Array
ユーザー gucci0512
提出日時 2023-10-27 23:29:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 4,029 bytes
コンパイル時間 3,922 ms
コンパイル使用メモリ 232,004 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-09-25 15:25:08
合計ジャッジ時間 10,488 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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;
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0