GCD Reduce


Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

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 N integers, 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个数,每次任意选取两个数,然后这两个数的值会变成gcd(a,b),如果能把整个序列都变成1的话,求选择的顺序;

思路:很明显只要做出1就行了,gcd(1,x)=1,我从第一个数开始一直向后面去gcd,取到第i个数肯定就是那么1的值前i个数的gcd,只要判断一直取到最后一个第1个数有没有变成1就行了,判断有之后,说明除了第一个数其他的n-1个数肯定会是有一个数跟第一个数的gcd为1,反证法就能很好的证明,那么只要第一个数跟另外的n-1个数依次取gcd,碰到互质就跳出来,并记录点,那么只要先选择1和这个点,剩下的n-2个数直接和1取就行了,但是这样交上去时wa的。。。你第一个数依次和后面的数取gcd那么取到最后一个的时候,1这个数肯定是前n个数的最大公约数,如果第一个数此时为1的话,那么再进行一次操作就好了,要求操作次数小于5*n,这个时候只是2*(n-1),完全是可以的,这样交上去是对的,反过来想,第一个数取到最后为1,那么最后一个数肯定这个时候也为1啊,我从后面往前GCD肯定也是对的,但是这样写的话交上去也是wa的,应该是special judge没有写好。。。

 #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 100100
int a[N];
int gcd(int a,int b)
{
if(b==) return a;
return gcd(b,a%b);
}
int main()
{
int n;
int cnt=;
while(scanf("%d",&n)!=EOF)
{
cnt++;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int res=a[];
for(int i=;i<=n;i++)
res=gcd(res,a[i]);
if(res!=)
printf("Case %d: -1\n\n",cnt);
else
{
printf("Case %d: %d\n",cnt,*(n-));
for(int i=;i<=n;i++)
printf("1 %d\n",i);
for(int i=;i<=n;i++)
printf("1 %d\n",i);
printf("\n");
}
}
return ;
}

ZOJ 3846 GCD Reduce//水啊水啊水啊水的更多相关文章

  1. zoj.3868.GCD Expectation(数学推导>>容斥原理)

    GCD Expectation Time Limit: 4 Seconds                                     Memory Limit: 262144 KB    ...

  2. Zoj 3868 GCD Expectation

    给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件 ...

  3. 【推导】zoj3846 GCD Reduce

    题意:给你n个正整数a1...an,一次操作是选择任意两个数ai,aj,将它们都替换成gcd(ai,aj).让你在5n步内将所有数变为1.或者输出不可能. 如果所有数的gcd不为1,显然不可能. 否则 ...

  4. ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)

    Description Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, ...

  5. ZOJ 3868 GCD Expectation (容斥+莫比乌斯反演)

    GCD Expectation Time Limit: 4 Seconds     Memory Limit: 262144 KB Edward has a set of n integers {a1 ...

  6. zoj[3868]gcd期望

    题意:求n个数组成的集合的所有非空子集的gcd的期望 大致思路:对于一个数x,设以x为约数的数的个数为cnt[x],所组成的非空集合个数有2^cnt[x]-1个,这其中有一些集合的gcd是x的倍数的, ...

  7. 有一个5ml 的瓶子 和3ml 的瓶子 和 很多水 现在 要取出4ml的水 请写出编程 多种解法

    //TODO public class demo { public static void main(String[] args) { demo.ss(); demo.sss(); } public ...

  8. ZOJ 2514 Generate Passwords 水

    啦啦啦,水一发准备去复习功课~ ------------------------------------------水一发的分割线----------------------------------- ...

  9. May Challenge 2019 Division 2 水题讲解

    Reduce to One 这题其实蛮水的? 题意就是说: 给定一个 1~n 的序列,每次挑两个数 x y 合并,合并值为 \(x+y+xy\) ,然后求不断合并最后剩下的一个的最大值 随便搞搞发现答 ...

随机推荐

  1. python线程中的全局变量与局部变量

    在python多线程开发中,全局变量是多个线程共享的数据,局部变量是各自线程的,非共享的. 如下几种写法都是可以的: 第一种:将列表当成参数传递给线程 from threading import Th ...

  2. Truncated Power Method for Sparse Eigenvalue Problems

    目录 算法 k的选择 \(x\)的初始化 代码 抱歉,真的没怎么看懂,当然,估计和我现在没法静下心来好好看也有关系. 算法 想法非常非常简单吧,就是在原来幂法的基础上,每次迭代的时候再加个截断.当然, ...

  3. PT与PX区别

    字体大小的设置单位,常用的有2种:px.pt.这两个有什么区别呢? 先搞清基本概念:px就是表示pixel,像素,是屏幕上显示数据的最基本的点: pt就是point,是印刷行业常用单位,等于1/72英 ...

  4. mysql 无法退出sql命令行编辑

    mysql 无法退出sql命令行编辑 | ANBOBhttp://www.anbob.com/archives/579.html mysql 无法退出sql命令行编辑 - 码农甲乙丙 - CSDN博客 ...

  5. Unit 2.前端之html--table(表格),form(表单)标签

    一.table标签 作用:定义html表格.一个table标签元素至少包含 thead(表头),tbody(表主题),还可以有tfoot(表底部) html表格游table元素及一个或者多个tr,th ...

  6. Flutter 中 JSON 解析

    本文介绍一下Flutter中如何进行json数据的解析.在移动端开发中,请求服务端返回json数据并解析是一个很常见的使用场景.Android原生开发中,有GsonFormat这样的神器,一键生成Ja ...

  7. 6s ios9.0平台 微信小程序的fixed定位兼容性问题

    如果不设置top和left的话  就会出现不显示问题

  8. js对字符串的一些操作方法

    1.charCodeAt(index); 返回一个整数,代表下标位置上字符的Unicode的编码. 2.fromCharCode(code1,code2,code3,...); code1代表Unic ...

  9. EXAMPLE FOR PEEWEE 多姿势使用 PEEWEE

    使用 PEEWEE 断断续续的差不多已经三个年头了,但是没有像这次使用这么多的特性和功能,所以这次一并记录一下,需要注意的地方和一些使用细节,之后使用起来可能会更方便. 因为是使用的 SQLAched ...

  10. 对于tomcat通过catalina.sh停止服务后,tomcat进程没有退出问题解决办法

    例:tomcat路径及名称为/data/apache-tomcat-7.0.67/ vim /data/apache-tomcat-7.0.67/bin/catalina.sh 找到org.apach ...