【无标题】

给定一个正整数数组,允许进行选择两个索引并用上取整除法更新其中一个元素的操作。问题是要确定是否能通过这些操作使所有数组元素变得相等。题目保证如果存在解决方案,那么在不超过30*n的操作次数内可以达到目标。输入包括测试用例数量、数组大小和元素,输出是操作次数及具体操作。示例中展示了不同情况下的处理过程。
摘要由CSDN通过智能技术生成

                                        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 4

Note

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值