RoAlgo Contest #8 | Jocuri cu Kdouri

This was the problem page during the contest. Access the current page here.
Time limit: 1s Memory limit: 64MB Input: Output:

Don't leave for tomorrow what you can do today, leave it for the day after tomorrow

Task

Alice Elice challenged Bob Glob to a game. Alice Elice has two piles with P1P_1 and P2P_2 gifts. Before the game starts, Alice rolls a die with NN faces and notes the resulting number as KK. The game consists of the following stages:

At the beginning, if KK is even, she opens all the gifts from the first pile, and if KK is odd, she opens all the gifts from the second pile. In the subsequent turns, they alternately pick gifts, starting with Bob. The player must open between 11 and KK gifts from the remaining pile. The player who cannot open at least one gift loses.

Task

Alice wants to have many gifts next year, so she wants to find out the probability of winning the game, assuming that both play optimally.

Input data

The first line contains TT, the number of tests. On the next TT lines, P1P_1, P2P_2, and NN are given.

Output data

For each of the TT tests, print the chance of winning modulo 109+710^9 + 7 (see below).

Constraints and clarifications

  • 0T1030 \leq T \leq 10^3
  • 0P1,P21090 \leq P_1, P_2 \leq 10^9
  • 0N1040 \leq N \leq 10^4
  • Assume there exists a model of a die with NN faces.
  • If we express the probability that Alice will win as the fraction ab\frac{a}{b}, then you will output ab1 mod (109+7)a \cdot b^{-1} \text{ mod } (10^9 + 7), where b1b^{-1} is the modular inverse of bb, modulo 109+710^9 + 7.
  • Both play optimally.
# Score Constraints
1 10 N=2N = 2, 0P1,P230 \leq P_1, P_2 \leq 3
2 30 N=6N = 6, 0P1,P2200 \leq P_1, P_2 \leq 20
3 60 No additional constraints

Example

stdin

3
3 2 3
333625 453145 800
1 1 1

stdout

0
855000006
0

Explanation

If the die shows the value 11:

  • The remaining pile is 11 with 33 gifts \rightarrow Alice loses;

If the die shows the value 22:

  • The remaining pile is 22 with 22 gifts \rightarrow Alice loses;

If the die shows the value 33:

  • The remaining pile is 11 with 33 gifts \rightarrow Alice loses.

Log in or sign up to be able to send submissions!