#P8457. 「SWTR-8」幂塔方程
「SWTR-8」幂塔方程
Background

The image comes from Solara570’s Bilibili video How dangerous is it to believe simple and intuitive conclusions too easily?.
One day long ago, A happened to come across this video on Bilibili. The infinite power tower equation in the video, and its “simple and intuitive, but full of traps” solution, left a deep impression on him.
Problem Description
If A were a “duliu” (dú liú, a nasty problem setter), he would make you solve a power tower equation with ten or even nine layers, but he is not.
He wants you to solve:
It is guaranteed that the largest prime factor of does not exceed , and is coprime with .
You must ensure that the solution you obtain is an integer in the range . If there is no solution in this range, output . If multiple solutions exist, output any one.
There are multiple test cases.
Input Format
The first line contains an integer , representing the Subtask ID of this test point.
The second line contains an integer , representing the number of test cases. Then follow test cases. Each test case is:
- The first line contains two integers .
- The second line contains several increasing primes , representing all prime factors of .
Output Format
Output lines, each being an integer in the range .
0
10
7 4
7
16 3
2
6 1
2 3
144 5
2 3
2520 11
2 3 5 7
999999 2
3 7 11 13 37
22511 21795
22511
47067727606562827 30911969774113407
3083 13697 25747 43291
2147483648 2333333
2
675288511488360000 510472780110265817
2 3 5 7 11
25
11
1
101
4811
219871229
16139671
760913896873844308082367046696111
1221598821
24445987958110300438937
Hint
"Constraints and Conventions"
This problem uses bundled tests.
- Subtask #1 (5 points): .
- Subtask #2 (8 points): . Depends on Subtask #1.
- Subtask #3 (11 points): is prime, .
- Subtask #4 (15 points): , .
- Subtask #5 (9 points): , . Depends on Subtask #4.
- Subtask #6 (13 points): , .
- Subtask #7 (7 points): , . Depends on Subtask #3, #6.
- Subtask #8 (6 points): the largest prime factor of does not exceed . Depends on Subtask #2.
- Subtask #9 (16 points): , .
- Subtask #10 (10 points): no special restrictions. Depends on Subtask #5, #7, #8, #9.
For of the testdata:
- .
- .
- , .
- .
"Help and Hints"
Contestants may determine when to stop reading prime factors of by trial dividing while reading.
"Source"
- Sweet Round 8 F.
- Idea & Solution: demonlover923 & codecode.
- Tester: Alex_Wei.
Update on 2025.5.30: For this problem, it is possible to have .
Translated by ChatGPT 5