当前位置:   article > 正文

zoj 3846 GCD Reduce(数论)

zoj 3846

题目链接

GCD Reduce

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

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 < jN);
  • 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 < jN), 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个数。每次操作步骤如下:

1,选取两个数,设它们的下标为i,j。(i<j)

2,将两个数替换为它们的最大公约数。

输出一种操作方案,使得最后所有的数都变成一,且操作数不超过5*n。

题意:1与所有正数的最大公约数都为1。所以我们操作出1,然后用这个1把所有数都变成1就可以了。先操作(1,2),然后(2,3),然后(3,4)......直到(n-1,n),这时第n个数就是n个数的最大公约数,如果第n个数不为1则无解。否则,再用最后一个数与前(n-1)个数操作一次,就可以把所有数都变成1了。总操作数2n-2次。

代码如下:

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string.h>
  5. #include<string>
  6. #include<queue>
  7. #include<stack>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<vector>
  12. #define inff 0x3fffffff
  13. #define nn 110000
  14. #define mod 1000000007
  15. typedef long long LL;
  16. const LL inf64=inff*(LL)inff;
  17. using namespace std;
  18. int n;
  19. int a[nn];
  20. vector<pair<int,int> >ans;
  21. int gcd(int x,int y)
  22. {
  23. if(y==0)
  24. return x;
  25. return gcd(y,x%y);
  26. }
  27. int main()
  28. {
  29. int i;
  30. int cas=1;
  31. while(scanf("%d",&n)!=EOF)
  32. {
  33. ans.clear();
  34. for(i=1;i<=n;i++)
  35. {
  36. scanf("%d",&a[i]);
  37. }
  38. int ix=a[1];
  39. int id=1;
  40. if(ix!=1)
  41. {
  42. for(i=2;i<=n;i++)
  43. {
  44. ix=gcd(ix,a[i]);
  45. ans.push_back(make_pair(i-1,i));
  46. if(ix==1)
  47. {
  48. id=i;
  49. break;
  50. }
  51. }
  52. }
  53. printf("Case %d: ",cas++);
  54. if(ix!=1)
  55. {
  56. puts("-1");
  57. }
  58. else
  59. {
  60. for(i=1;i<=n;i++)
  61. {
  62. if(i==id)
  63. continue;
  64. if(i<id)
  65. ans.push_back(make_pair(i,id));
  66. else
  67. ans.push_back(make_pair(id,i));
  68. }
  69. int la=ans.size();
  70. printf("%d\n",la);
  71. for(i=0;i<la;i++)
  72. {
  73. printf("%d %d\n",ans[i].first,ans[i].second);
  74. }
  75. }
  76. puts("");
  77. }
  78. return 0;
  79. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/75465
推荐阅读
相关标签
  

闽ICP备14008679号