#P5345. 【XR-1】快乐肥宅

【XR-1】快乐肥宅

Description

Each happy chubby guy’s weight on day 00 is 11. Let the ii-th happy chubby guy’s weight be wiw_i, so initially wi=1w_i = 1.

The ii-th happy chubby guy has a personal happiness index kik_i, meaning that every day right after he wakes up, his weight becomes kik_i times his weight from the previous day.

These chubby guys are determined. The ii-th chubby guy has a personal awakening weight gig_i, meaning that once his weight is greater than gig_i, he will go to the gym. Each time, he reduces his weight by gig_i, until his weight is less than or equal to gig_i.

After working out, they meet in the computer lab, and they find that sometimes their weights become very interesting.

One day, they found that their weights formed an arithmetic progression.

Another day, they found that their weights formed a geometric progression!

The chubby guys wonder: if the weights of the nn happy chubby guys {w1,w2,,wn}\{w_1, w_2, \ldots, w_n\} exactly form the sequence {r1,r2,,rn}\{r_1, r_2, \ldots, r_n\}, what is the minimum number of days needed?

However, if they wait for a very long time and still do not reach that day, they will think it is impossible.

Input Format

The first line contains a positive integer nn.

The next nn lines each contain three positive integers ki,gi,rik_i, g_i, r_i, representing the ii-th chubby guy’s happiness index, awakening weight, and the target weight in the sequence.

Output Format

If the given sequence is formed before the end of day 10910^9, output one number: the minimum number of days required.

If the given sequence is not formed before the end of day 10910^9, output Impossible.

2
4 7 4
2 5 3
7
2
4 7 3
2 5 3
Impossible
2
4 7 1
2 5 1
0
3
14 60 44
6 50 6
1029 91287 87318
101
1
6 65536 65536
16
2
2 2 2
2 3 1

2

Hint

[Sample 11 Explanation]

The table below shows the weight changes of two chubby guys from day 00 to day 77:

Days Chubby guy 11’s weight Chubby guy 22’s weight Explanation
00 11 On day 00, each chubby guy’s weight is 11
11 44 22 Chubby guy 11’s weight is 44 times the previous day, and chubby guy 22’s weight is 22 times the previous day
22 44 Chubby guy 11’s weight is 44 times the previous day, which is 1616. He finds his weight exceeds 77, so he goes to the gym twice, reducing his weight by 2×7=142\times 7=14
33 11 33 On this day, both chubby guy 11 and chubby guy 22 go to the gym once
44 11 Chubby guy 22 goes to the gym once
55 22 Chubby guy 11 goes to the gym twice
66 11 44 Chubby guy 11 goes to the gym once
77 44 33 Chubby guy 22 goes to the gym once

It can be seen that on day 77, the chubby guys’ weights form the sequence {4,3}\{4, 3\}.

[Constraints and Notes]

Subtask 1 (20 points): n50n \le 50, gi50g_i \le 50.
Subtask 2 (20 points): gig_i is prime.
Subtask 3 (20 points): gi103g_i \le 10^3.
Subtask 4 (20 points): ri{1,gi}r_i \in \{1, g_i\}.
Subtask 5 (20 points): no special constraints.

For 100%100\% of the testdata: 1n1031 \le n \le 10^3, 1ki,rigi1071 \le k_i, r_i \le g_i \le 10^7.

Translated by ChatGPT 5