#include #include #include using namespace std; // ======================================================================================= 最大 135 桁の多倍長整数 struct BigInteger { vector val; BigInteger() { val = vector(15, 0); } BigInteger(string str) { val = vector(15, 0); vector keisuu(15, 1); reverse(str.begin(), str.end()); for (int i = 0; i < str.size(); i++) { val[i / 9] += keisuu[i / 9] * (int)(str[i] - '0'); keisuu[i / 9] *= 10; } } }; bool operator>= (const BigInteger& a1, const BigInteger& a2) { for (int i = 14; i >= 0; i--) { if (a1.val[i] > a2.val[i]) return true; if (a1.val[i] < a2.val[i]) return false; } return true; } bool operator> (const BigInteger& a1, const BigInteger& a2) { for (int i = 14; i >= 0; i--) { if (a1.val[i] > a2.val[i]) return true; if (a1.val[i] < a2.val[i]) return false; } return false; } bool operator< (const BigInteger& a1, const BigInteger& a2) { for (int i = 14; i >= 0; i--) { if (a1.val[i] < a2.val[i]) return true; if (a1.val[i] > a2.val[i]) return false; } return false; } BigInteger operator+(const BigInteger& a1, const BigInteger& a2) { BigInteger a3; for (int i = 0; i < 15; i++) a3.val[i] = a1.val[i] + a2.val[i]; for (int i = 0; i < 14; i++) { if (a3.val[i] >= 1000000000) { a3.val[i] -= 1000000000; a3.val[i + 1] += 1; } } return a3; } BigInteger operator-(const BigInteger& a1, const BigInteger& a2) { BigInteger a3; for (int i = 0; i < 15; i++) a3.val[i] = a1.val[i] - a2.val[i]; for (int i = 0; i < 14; i++) { if (a3.val[i] < 0) { a3.val[i] += 1000000000; a3.val[i + 1] -= 1; } } return a3; } BigInteger operator<<(const BigInteger& a1, const int& a2) { BigInteger a3; for (int i = 0; i < 15; i++) a3.val[i] = a1.val[i]; for (int i = 0; i < a2; i++) { for (int j = 0; j < 15; j++) a3.val[j] <<= 1; for (int j = 0; j < 14; j++) { if (a3.val[j] >= 1000000000) { a3.val[j] -= 1000000000; a3.val[j + 1] += 1; } } } return a3; } BigInteger operator>>(const BigInteger& a1, const int& a2) { BigInteger a3; for (int i = 0; i < 15; i++) a3.val[i] = a1.val[i]; for (int i = 0; i < a2; i++) { for (int j = 0; j < 14; j++) { if ((a3.val[j + 1] & 1) == 1) { a3.val[j + 1] -= 1; a3.val[j] += 1000000000; } a3.val[j] >>= 1; } } return a3; } BigInteger Abs(BigInteger a, BigInteger b) { if (a >= b) return a - b; return b - a; } // ======================================================================================= メイン部分 void Operation(vector& A, int p1, int p2) { BigInteger Avg = (A[p1] + A[p2]) >> 1; A[p1] = Avg; A[p2] = Avg; } int main() { BigInteger Target; Target.val[11] = 5; vector> Answer; // Step 1. 入力 int N; cin >> N; vector P(N); for (int i = 0; i < N; i++) { string str; cin >> str; P[i] = BigInteger(str); } int StartTime = clock(); BigInteger MinScore = Target; // Step 2. シミュレーション while (clock() - StartTime < 9 * CLOCKS_PER_SEC / 10) { vector A = P; vector> CurrentAns; // 最初に 1 回操作 pair Minimum1 = make_pair(Target, -1); for (int i = 1; i < N; i++) { BigInteger Avg = (A[0] + A[i]) >> 1; Minimum1 = min(Minimum1, make_pair(Abs(Avg, Target), i)); } Operation(A, 0, Minimum1.second); CurrentAns.push_back(make_pair(0, Minimum1.second)); // 貪欲法 BigInteger Remain = (Target << (N - 1)) - (A[0] << (N - 2)); vector Order; vector Used(N, false); Used[0] = true; Order.push_back(0); for (int i = N - 3; i >= 1; i--) { vector> Vec; for (int j = 0; j < N; j++) { if (Used[j] == true) continue; if (Remain < (A[j] << i)) continue; BigInteger Score = Abs(Remain, A[j] << (i + 1)); Vec.push_back(make_pair(Score, j)); } sort(Vec.begin(), Vec.end()); if (Vec.size() == 0) break; int idx = Vec[rand() % max(1, (int)Vec.size() / 3)].second; Order.push_back(idx); Used[idx] = true; Remain = Remain - (A[idx] << i); } for (int i = 0; i < N; i++) { if (Used[i] == false) Order.push_back(i); } // 答えを得る for (int i = N - 2; i >= 0; i--) { CurrentAns.push_back(make_pair(Order[i], Order[i + 1])); Operation(A, Order[i], Order[i + 1]); } // 現在のスコアと比較 if (MinScore > Abs(Target, A[0])) { MinScore = Abs(Target, A[0]); Answer = CurrentAns; } } // Step 5. 出力 cout << Answer.size() << endl; for (int i = 0; i < Answer.size(); i++) cout << Answer[i].first + 1 << " " << Answer[i].second + 1 << endl; return 0; }