結果

問題 No.2519 Coins in Array
ユーザー kotatsugamekotatsugame
提出日時 2023-10-27 21:53:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 2,158 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 81,224 KB
実行使用メモリ 14,228 KB
最終ジャッジ日時 2023-10-27 21:53:46
合計ジャッジ時間 4,519 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 0 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 48 ms
11,588 KB
testcase_24 AC 49 ms
11,588 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 51 ms
11,712 KB
testcase_28 AC 50 ms
11,712 KB
testcase_29 AC 50 ms
11,712 KB
testcase_30 AC 45 ms
11,160 KB
testcase_31 AC 9 ms
4,636 KB
testcase_32 AC 25 ms
7,444 KB
testcase_33 AC 36 ms
9,944 KB
testcase_34 AC 17 ms
5,944 KB
testcase_35 AC 32 ms
8,408 KB
testcase_36 AC 14 ms
5,616 KB
testcase_37 AC 7 ms
4,396 KB
testcase_38 AC 27 ms
7,728 KB
testcase_39 AC 15 ms
5,788 KB
testcase_40 AC 43 ms
14,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cassert>
using namespace std;
long long gcd(long long a,long long b)
{
	while(b)
	{
		long long t=a%b;
		a=b;
		b=t;
	}
	return a;
}
long long f(long long x,long long y)
{
	long long g=gcd(x,y);
	if(g>=2)return 0LL;
	x--,y--;
	assert(x==0||y==0||x<=(long long)1e18/y);
	return x*y;
}
vector<pair<int,int> >ans;
long long solve(vector<long long>A)
{
	if(A.size()==1)return A[0];
	int zeroi=-1,onei=-1;
	vector<int>EV,OD;
	for(int i=0;i<A.size();i++)
	{
		if(A[i]==0)zeroi=i;
		else if(A[i]==1)onei=i;
		else
		{
			if(A[i]%2==0)EV.push_back(i);
			else OD.push_back(i);
		}
	}
	if(zeroi!=-1)
	{
		if(zeroi!=A.size()-1)
		{
			ans.push_back(make_pair(zeroi,A.size()-1));
			A.pop_back();
			A.erase(A.begin()+zeroi);
			A.push_back(0);
			return solve(A);
		}
		for(int i=A.size()-1;i--;)
		{
			ans.push_back(make_pair(i,i+1));
		}
		return 0LL;
	}
	if(onei!=-1)
	{
		if(onei!=A.size()-1)
		{
			ans.push_back(make_pair(onei,A.size()-1));
			A.pop_back();
			A.erase(A.begin()+onei);
			A.push_back(0);
			return solve(A);
		}
		for(int i=A.size()-1;i--;)
		{
			ans.push_back(make_pair(i,i+1));
		}
		return 0LL;
	}
	if(EV.size()>=2)
	{
		ans.push_back(make_pair(EV[0],EV[1]));
		A.erase(A.begin()+EV[1]);
		A.erase(A.begin()+EV[0]);
		A.push_back(0);
		return solve(A);
	}
	if(EV.size()==1&&OD.size()>=2||OD.size()>=4)
	{
		long long x=OD[0],y=OD[1];
		ans.push_back(make_pair(OD[0],OD[1]));
		A.push_back(f(A[OD[0]],A[OD[1]]));
		A.erase(A.begin()+OD[1]);
		A.erase(A.begin()+OD[0]);
		return solve(A);
	}
	assert(A.size()<=3);
	if(A.size()==2)
	{
		ans.push_back(make_pair(0,1));
		return f(A[0],A[1]);
	}
	long long x=A[0],y=A[1],z=A[2];
	long long X=f(f(x,y),z),Y=f(f(y,z),x),Z=f(f(z,x),y);
	if(X<=Y&&X<=Z)ans.push_back(make_pair(0,1));
	else if(Y<=X&&Y<=Z)ans.push_back(make_pair(1,2));
	else ans.push_back(make_pair(0,2));
	ans.push_back(make_pair(0,1));
	return min(min(X,Y),Z);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	vector<long long>A(N);
	for(int i=0;i<N;i++)cin>>A[i];
	cout<<solve(A)<<"\n";
	for(pair<int,int>p:ans)cout<<p.first+1<<" "<<p.second+1<<"\n";
}
0