GCD Reduce

Description

You are given a sequence {A1A2, ..., AN}. You task is to change all the element of the sequence to 1 with the following operations (you may need to apply it multiple times):

  • choose two indexes i and j (1 ≤ i < j ≤ N);
  • change both Ai and Aj to gcd(AiAj), where gcd(AiAj) is the greatest common divisor of Ai and Aj.

You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5N operations.

Input

Input will consist of multiple test cases.

The first line of each case contains one integer N (1 ≤ N ≤ 105), indicating the length of the sequence. The second line contains Nintegers, A1A2, ..., AN (1 ≤ Ai ≤ 109).

Output

For each test case, print a line containing the test case number (beginning with 1) followed by one integer M, indicating the number of operations needed. You must assure that M is no larger than 5N. If you cannot find a solution, make M equal to -1 and ignore the following output.

In the next M lines, each contains two integers i and j (1 ≤ i < j ≤ N), indicating an operation, separated by one space.

If there are multiple answers, you can print any of them.

Remember to print a blank line after each case. But extra spaces and blank lines are not allowed.

Sample Input

4
2 2 3 4
4
2 2 2 2

Sample Output

Case 1: 3
1 3
1 2
1 4

Case 2: -1

题意:给出n个数,试着求出这n个数最后的最大公约数是否为1;若果为1,那么给出两两匹配的顺序(次数不能超出5*n);反之,输出-1;

我们先判断n个数的最大公约数是否为1.即一个数与其他所有数求公约数;看最后是否是1.若果为1那么最后输出的时候。输出两遍即可;输出的次数为2*n-2;输出格式不唯一;

标程:

# include <iostream>
# include <cstdio>
using namespace std;

const int maxn = 1e5 + 10;

int a[maxn];

int gcd(int a,int b)
{
    if(b)   return gcd(b,a%b);  //如果b不为0;
    return a;
}

int main()
{
    int kase=0,n;
    while(~scanf("%d",&n)){
        for(int i = 0; i < n;i++)   scanf("%d",&a[i]);
        int temp = a[0];
        for(int i = 1;i < n;i++)    temp = gcd(temp,a[i]);  //找出所有数的最大公约数;
        printf("Case %d: ",++kase);
        if(temp != 1){
            puts("-1");
            puts("");
        }
        else{
            printf("%d\n",2*n-2);
            for(int i=1;i<n;i++)    printf("1 %d\n",i+1);
            for(int i=1;i<n;i++)    printf("1 %d\n",i+1);
            puts("");
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值