Given a character and a sequence of monsters generated by recurrence, decide whether the character survives all N fights in order and print the final stats or -1.
Medium6SimulationMathImplementationNumber theoryNo attempts yetTime limit1.5sMemory limit512 MBYeongwoo is playing a game called God4 again. In God4 every character and every monster has an attack A, a defence D, and a maximum health H. Combat is always one on one and runs by these rules.
When the character wins, it absorbs the monster's stats exactly as they are. A character with stats (5,5,5) that beats a monster with stats (1,1,1) ends up with (6,6,6). Every win also restores the character's health to its maximum, so every fight starts at full health.
Yeongwoo wants to beat N monsters in a row without a single loss, which means he has to win every fight. Listing the data of all N monsters would make the input far too large, so the stats of the k-th monster for k>1 come from these formulas.
Ak=((Ak−1×Ap+Aa)mod100)+1
Dk=((Dk−1×Dp+Da)mod3)+1
Hk=((Hk−1×Hp+Ha)mod1000)+1
The program reads from standard input.
The first line contains the number of monsters N. (1≤N≤5×107)
The second line contains the attack A, the defence D, and the maximum health H of Yeongwoo's character, in that order. (1≤A≤100, 1≤D≤3, 1≤H≤1000)
The third line contains the attack A1, the defence D1, and the maximum health H1 of the first monster, in that order. (1≤A1≤100, 1≤D1≤3, 1≤H1≤1000)
The fourth line contains the values Ap, Aa, Dp, Da, Hp, Ha used to generate the monster stats, in that order. (1≤Ap,Dp,Hp≤109, 0≤Aa≤99, 0≤Da≤2, 0≤Ha≤999)
The program writes to standard output.
If Yeongwoo beats all N monsters, print the character's final stats on one line in the order attack, defence, maximum health, separated by single spaces. Print each of the three values modulo 109+7.
If he loses even once, print -1 on the first line and nothing else.