B. Equalize by Divide
You are given an array 𝑎1,𝑎2,…,𝑎𝑛 of positive integers.
You can make this operation multiple (possibly zero) times:
- Choose two indices 𝑖, 𝑗 (1≤𝑖,𝑗≤𝑛).
- Assign 𝑎𝑖:=⌈𝑎𝑖/𝑎𝑗⌉. Here ⌈𝑥⌉denotes 𝑥 rounded up to the smallest integer ≥𝑥≥.
Is it possible to make all array elements equal by some sequence of operations (possibly empty)? If yes, print any way to do it in at most 30𝑛 operations.
It can be proven, that under the problem constraints, if some way exists to make all elements equal, there exists a way with at most 30𝑛 operations.
Input
The first line contains a single integer 𝑡 (1≤𝑡≤1000) — the number of test cases. Descriptions of test cases follow.
The first line of each test case description contains a single integer 𝑛 (1≤𝑛≤100).
The second line of each test case description contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109).
It is guaranteed, that the sum of 𝑛for all test cases does not exceed 10001000.
Output
For each test case print a single integer 𝑞 (−1≤𝑞≤30). If 𝑞=−1, there is no solution, otherwise 𝑞 is equal to the number of operations.
If 𝑞≥0, on the next 𝑞 lines print two integers 𝑖, 𝑗 (1≤𝑖,𝑗≤𝑛) — descriptions of operations.
If there are multiple solutions, you can print any.
Example
input
Copy
10
1
100
3
1 1 1
2
2 1
2
5 5
3
4 3 2
4
3 3 4 4
2
2 100
5
5 3 6 7 8
6
3 3 80 3 8 3
4
19 40 19 55
output
Copy
0 0 -1 0 2 1 3 2 1 4 3 1 4 2 1 3 2 4 6 2 1 2 1 2 1 2 1 2 1 2 1 8 5 2 4 2 3 2 1 3 1 3 2 1 4 1 5 1 4 5 1 3 1 3 1 3 1 9 4 2 2 1 1 2 1 2 3 2 3 2 1 4 2 4 3 4Note
In the first and second, fourth test cases all numbers are equal, so it is possible to do nothing.
In the third test case, it is impossible to make all numbers equal.
In the fifth test case: [4,3,2]→[2,3,2]→[2,2,2]
In the sixth test case: [3,3,4,4]→[3,3,2,4]→[3,3,2,2]→[2,3,2,2]→[2,2,2,2]
Here the red numbers are 𝑖indices (that will be assigned), blue numbers are 𝑗 indices.
题意为通过将一个数除另一个数上取整,如此重复,如果能使最后所有数都相等,就输出yes,反之NO;
#include <iostream> #include<algorithm> #include<queue> using namespace std; typedef long long ll; typedef pair<int,int> p; const int N=110,P=131; int t,n; struct A { int num,x; }a[N]; bool cmp(struct A a,struct A b ){ return a.x<b.x; } int main(){ scanf("%d",&t); while(t--) { scanf("%d", &n); for (int i = 1; i <= n; i++) { cin >> a[i].x; a[i].num = i; } sort(a + 1, a + 1 + n, cmp); int cnt = 0; queue<p> q; if (a[1].x == a[n].x) printf("0\n"); else if (a[1].x == 1) printf("-1\n"); else { while (a[1].x != a[n].x) { a[n].x = (a[n].x + a[1].x - 1) / a[1].x; p s; s.first = a[n].num; s.second = a[1].num; q.push(s); cnt++; sort(a + 1, a + 1 + n, cmp); } cout << cnt << endl; while (q.size()) { int ff = q.front().first, ss = q.front().second; printf("%d %d\n", ff, ss); q.pop(); } } } return 0; }