結果
問題 | No.2519 Coins in Array |
ユーザー |
![]() |
提出日時 | 2023-10-27 23:03:52 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 51 ms / 2,000 ms |
コード長 | 2,819 bytes |
コンパイル時間 | 4,370 ms |
コンパイル使用メモリ | 254,652 KB |
最終ジャッジ日時 | 2025-02-17 15:47:12 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 37 |
ソースコード
#include<bits/stdc++.h>using namespace std;//* ATCODER#include<atcoder/all>using namespace atcoder;typedef modint998244353 mint;//*//* BOOST MULTIPRECISION#include<boost/multiprecision/cpp_int.hpp>using namespace boost::multiprecision;//*/typedef long long ll;#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)template <typename T> bool chmin(T &a, const T &b) {if (a <= b) return false;a = b;return true;}template <typename T> bool chmax(T &a, const T &b) {if (a >= b) return false;a = b;return true;}template <typename T> T max(vector<T> &a){assert(!a.empty());T ret = a[0];for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);return ret;}template <typename T> T min(vector<T> &a){assert(!a.empty());T ret = a[0];for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);return ret;}template <typename T> T sum(vector<T> &a){T ret = 0;for (int i=0; i<(int)a.size(); i++) ret += a[i];return ret;}int main(){ios_base::sync_with_stdio(false);cin.tie(NULL);auto f = [&](ll x, ll y) -> ll {if (x == 1 || y == 1){return 0;}if (x == 0 || y == 0){return 0;}if (__gcd(x, y) != 1){return 0;}return (x-1) * (y-1);};int n; cin >> n;vector<ll> a(n);rep(i,0,n){cin >> a[i];}if (n == 2){cout << f(a[0], a[1]) << '\n';cout << 1 << ' ' << 2 << '\n';return 0;}else if (n == 3){if (f(a[0], f(a[1], a[2])) >= f(a[2], f(a[0], a[1]))&& f(a[1], f(a[0], a[2])) >= f(a[2], f(a[0], a[1]))){cout << f(a[2], f(a[0], a[1])) << '\n';cout << 1 << ' ' << 2 << '\n';cout << 1 << ' ' << 2 << '\n';}else if (f(a[1], f(a[0], a[2])) >= f(a[0], f(a[1], a[2]))&& f(a[2], f(a[0], a[1])) >= f(a[0], f(a[1], a[2]))){cout << f(a[0], f(a[1], a[2])) << '\n';cout << 2 << ' ' << 3 << '\n';cout << 1 << ' ' << 2 << '\n';}else{cout << f(a[1], f(a[0], a[2])) << '\n';cout << 1 << ' ' << 3 << '\n';cout << 1 << ' ' << 2 << '\n';}return 0;}else{// 確実に 1, 0 のいずれかになるvector<pair<int,int>> ans;ans.push_back(pair(1, 2));ans.push_back(pair(1, 2));{vector<ll> newvec(n-1);rep(i,2,n){newvec[i-2] = a[i];}newvec[n-2] = f(a[0], a[1]);a = newvec;}{vector<ll> newvec(n-2);rep(i,2,n-1){newvec[i-2] = a[i];}newvec[n-3] = f(a[0], a[1]);a = newvec;}{int piv = n-4;ll v = f(a[n-4], a[n-3]);ans.push_back(pair(piv + 1, piv + 2));piv--;while(piv >= 0){v = f(v, a[piv]);ans.push_back(pair(piv + 1, piv + 2));piv--;}cout << v << '\n';}assert((int)ans.size() == n-1);rep(i,0,n-1){cout << ans[i].first << ' ' << ans[i].second << '\n';}}}