#P15979. [PA 2026] 手机游戏 / Gra mobilna
[PA 2026] 手机游戏 / Gra mobilna
Problem Description
Recently in Bajtocja, a mobile game called Potyczki Survival Shooter has become very popular. In this game, we control a squad of warriors moving along a straight path. From time to time, the squad encounters an event. In the -th event, the player has two options: go on the left side of the road and gain additional warriors; or go on the right side and multiply the current number of warriors by . Although everyone knows that having more warriors is better, the game’s ads clearly state that making the right decision can sometimes be very complicated.
After the first events, Bajtazar has warriors. (The number does not necessarily have to be obtainable from the first events according to the rules above—after all, these are warriors, not tourists, and a bad player may lose some of them in fights with zombies; those rules are not described in this problem.) He wants to know how many warriors he will have after event if he plays optimally. Please help him compute it.
Input Format
The first line contains integers and (, ).
The next lines describe the events; the -th line contains two integers and (, ).
The next lines describe the queries; the -th line contains three integers , , and (, ).
Output Format
For each query, output one number: the number of warriors when using the optimal strategy. Output the result modulo .
10 2
3 2
3 2
3 2
0 1000
0 1000
0 1000
0 1000
0 1000
0 1000
123 1
1 0 3
1 3 9
16
49
Hint
Sample explanation: In the first query, Bajtazar starts with warrior. In the first event, he can gain additional warriors, or multiply the number of warriors by . Under the optimal strategy, he chooses the first option, so he will have warriors. The next two events are the same, but this time doubling the number of warriors is more beneficial. In the end, Bajtazar will have warriors.
Translated by ChatGPT 5