CCC '21 S5 - Math Homework

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 3.0s
Memory limit: 1G

Author:
Problem types
Canadian Computing Competition: 2021 Stage 1, Senior #5

Your math teacher has given you an assignment involving coming up with a sequence of N integers A1,,AN, such that 1Ai1000000000 for each i.

The sequence A must also satisfy M requirements, with the ith one stating that the GCD (Greatest Common Divisor) of the contiguous subsequence AXi,,AYi (1XiYiN) must be equal to Zi. Note that the GCD of a sequence of integers is the largest integer d such that all the numbers in the sequence are divisible by d.

Find any valid sequence A consistent with all of these requirements, or determine that no such sequence exists.

Input Specification

The first line contains two space-separated integers, N and M. The next M lines each contain three space-separated integers, Xi, Yi, and Zi (1iM).

The following table shows how the available 15 marks are distributed.

Subtask N M Zi
3 marks 1N2000 1M2000 1Zi2 for each i
4 marks 1N2000 1M2000 1Zi16 for each i
8 marks 1N150000 1M150000 1Zi16 for each i

Note: an additional test case worth 1 point was added to prevent unintended solutions from passing.

Output Specification

If no such sequence exists, output the string Impossible on one line. Otherwise, on one line, output N space-separated integers, forming the sequence A1,,AN. If there are multiple possible valid sequences, any valid sequence will be accepted.

Sample Input 1

Copy
2 2
1 2 2
2 2 6

Output for Sample Input 1

Copy
4 6

Explanation of Output for Sample Input 1

If A1=4 and A2=6, the GCD of [A1,A2] is 2 and the GCD of [A2] is 6, as required. Please note that other outputs would also be accepted.

Sample Input 2

Copy
2 2
1 2 2
2 2 5

Output for Sample Input 2

Copy
Impossible

Explanation of Output for Sample Input 2

There exists no sequence [A1,A2] such that the GCD of [A1,A2] is 2 and the GCD of [A2] is 5.


Comments


  • 3
    maxcruickshanks  commented on Jan. 1, 2024, 7:16 p.m.

    Since the original data were weak, an additional test case worth 1 point was added.


    • 0
      31501357  commented on Oct. 29, 2024, 3:42 a.m.

      My solution was much faster on the additional test case.