Friday, February 1, 2019

Dream Macro

  • Problem Description

    In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.

    When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1,a2,...,an. He remembered that he calculated gcd(ai,ai+1,...,aj) for every 1<=i<=j<=n and put it into a set S. gcd here means the greatest common divisor.

    Note that even if a number is put into the set S twice or more, it only appears once in the set.

    Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.

    Note
    In the first example 2=gcd(4,6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(ai,ai+1,...,aj) for every 1<=i<=j<=n.
  • CODING ARENA
  • n=int(input())
    a=list(map(int,input().split()))
    for i in range(1,n):
        if a[i]%a[0]:exit(print(-1))
    print(2*n)
    for i in range(n):print(a[0],a[i],end=' ')
  • Test Case 1

    Input (stdin)
    4
    
    2 4 6 12
    
    
    Expected Output
    8
    
    2 2 2 4 2 6 2 12
  • Test Case 2

    Input (stdin)
    2
    
    2 3
    
    
    Expected Output
    -1

No comments:

Post a Comment