zoj 3846 GCD Reduce
来源:互联网 发布:诽谤方志敏网络文章 编辑:程序博客网 时间:2024/05/20 01:10
You are given a sequence {A1, A2, ..., 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(Ai, Aj), where gcd(Ai, Aj) 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, A1, A2, ..., 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
42 2 3 442 2 2 2
Sample Output
Case 1: 31 31 21 4Case 2: -1
#include<cstdio>
#include<algorithm>
using namespace std;
int gcd(int a,int b){
if(a<b)
swap(a,b);
while(b!=0){
int r=b;
b=a%b;
a=r;
}
return a;
}
int main(){
int n,a[500008],order=1;
while(cin>>n){
for(int i=0;i<n;i++)
cin>>a[i];
int tmp=a[0];
for(int i=1;i<n;i++)
tmp=gcd(tmp,a[i]);
if(tmp!=1){
printf("Case %d: -1\n\n",order++);
}
else{
printf("Case %d: %d\n",order++,2*(n-1));
for(int i=2;i<=n;i++)
printf("1 %d\n",i);
for(int i=2;i<=n;i++)
printf("1 %d\n",i);
cout<<endl;
}
}
return 0;
}
- ZOJ 3846 GCD Reduce
- zoj 3846 GCD Reduce
- Zoj 3846-GCD Reduce(数论)
- ZOJ 3846GCD Reduce(数学)
- zoj 3846 GCD Reduce(数论)
- ZOJ(3846)——GCD Reduce
- ZOJ :: GCD Reduce
- ZOJ 4846 GCD Reduce (数学分析题)
- 【ZOJ】4846 GCD Reduce(求n个数的最大公约数)
- GCD Reduce
- ZOJ3846 GCD Reduce 数论
- zoj 1577 GCD & LCM
- ZOJ 1577 GCD & LCM
- ZOJ 1577 GCD & LCM
- ZOJ 1577 - GCD & LCM
- ZOJ 1577 GCD & LCM
- zoj 1577 GCD & LCM
- ZOJ 3868 GCD Expectation
- java编码原理 -- 源文件编译和执行
- Codeforces India Hack2016 653ABCED
- 最新版 CocoaPods 的安装步骤
- 自定义ANDROID中EDITTEXT中的HINT文本的大小
- TextView动态设置字体
- zoj 3846 GCD Reduce
- ViewDragHelper详解
- 大话模式学习笔记-代码无错就是优?-简单工厂模式
- IOS学习之——关闭ios虚拟键盘的几种方法
- JS 保留n位小数点
- 拾遗与填坑《深度探索C++对象模型》3.2节
- andrid实践项目四-对话框Dialog的各种用法
- 开博第一天,想写点有意义的东西!
- Android使用ViewDragHelper实现仿QQ6.0侧滑界面(一)