ZOJ 3846 GCD reduce//water AH water water AH water

Source: Internet
Author: User
Tags greatest common divisor

GCD Reduce Time limit: 2 Seconds Memory Limit: 65536 KB Special Judge

You are given a sequence { A 1, A 2, ..., A N }. You are to change all the element of the sequence to 1 with the following operations (you may need to apply it MULTIPL E times):

    • Choose Indexes and i j (1≤ i < j ≤ N );
    • Change both A i A j and to GCD ( A i , A j ), where GCD ( A i ,) is the A j GRE Atest common divisor of and A i A j .

You don't need to minimize the number of used operations. However, you need to make sure that there is at the most 5 N operations.

Input

Input would consist of multiple test cases.

The first line of all case contains one integer N (1≤ N ≤105), indicating the length of the sequence. The second line contains N integers, A 1, A 2, ..., A N (1≤ A i ≤109).

Output

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

Min the next lines, each contains. Integers i j and (1≤ i < j ≤ N ), indicating an op Eration, separated by one space.

If There is multiple answers, you can print any of them.

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

Sample Input
42 2 3 442 2 2 2
Sample Output
Case 1:31 4Case 2:1

Test instructions: Give you the number of n, each time arbitrarily select two number, then the value of the two numbers will become gcd (A, B), if the whole sequence can be changed to 1, the order of choice;

Idea: It is obvious that as long as the 1 is done, gcd (1,x) = 1, I start from the first number to go to the back gcd, take the number i is definitely 1 of the value of the first I number of GCD, as long as the judgment has been taken to the last 1th number has not become 1 on the line, judging there, Description In addition to the first number of other n-1 number will certainly be a number with the first number of GCD is 1, the rebuttal method can be very good proof, then as long as the first number with the other n-1 the number of GCD, encounter coprime jump out, and record points, then as long as the first choice 1 and this point, The remaining number of n-2 directly and 1 to take the line, but this turn up when WA's ... Your first number in order and after the number of GCD then take to the last time, 1 This number is definitely the first n number of greatest common divisor, if the first number at this time is 1, then another operation is good, requires less than 5*n operation times, this time is only a (n-1), is completely possible, This is right, in turn, think, the first number to take the last 1, then the last number is certainly this time also for 1 ah, I from the back forward gcd certainly is right, but this writing is also WA, should be special judge not written well ...

1#include <stdio.h>2#include <string.h>3#include <iostream>4 using namespacestd;5 #defineN 1001006 intA[n];7 intgcdintAintb)8 {9     if(b==0)returnA;Ten     returnGCD (b,a%b); One } A intMain () - { -     intN; the     intCnt=0; -      while(SCANF ("%d", &n)! =EOF) -     { -cnt++; +          for(intI=1; i<=n;i++) -scanf"%d",&a[i]); +         intres=a[1]; A          for(intI=2; i<=n;i++) atres=gcd (Res,a[i]); -         if(res!=1) -printf"Case %d: -1\n\n", CNT); -         Else -         { -printf"Case %d:%d\n"Cnt2* (n1)); in              for(intI=2; i<=n;i++) -printf"1%d\n", i); to              for(intI=2; i<=n;i++) +printf"1%d\n", i); -printf"\ n"); the         } *     } $     return 0;Panax Notoginseng}
View Code

ZOJ 3846 GCD reduce//water AH water water AH water

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.