Compute the expected number of draws until a blue lot has been drawn K times, where red lots are removed and green and blue lots are returned.
Hard9ProbabilityMathDynamic programmingNo attempts yetTime limit2sMemory limit512 MBHe draws lots whenever he has time to spare. Drawing them with no rules attached got boring, so he settled on rules that depend on the color of the lot he draws.
He has R lots with a red tip, G lots with a green tip, and B lots with a blue tip. He puts every lot into a box with the colored end hidden, mixes the box well, and draws one lot at a time. He mixes the box thoroughly every time, so every lot still in the box is equally likely to be drawn. He acts on the color of the lot he drew as follows.
Write a program that computes the expected number of lots he draws before he goes to sleep.
The first line contains the number of test cases T (1≤T≤103).
Each test case is one line holding the number of red lots R, the number of green lots G, the number of blue lots B, and the number of blue draws K that sends him to sleep, separated by spaces. All four numbers are integers between 1 and 109.
For each test case, print the expected number of lots he draws before he goes to sleep on its own line.
When the expected value is written as the irreducible fraction a/b, print the remainder of a×b−1 divided by 1,000,000,007. Here b−1 is the multiplicative inverse of b modulo 1,000,000,007. An answer exists for every input.
For R=1, G=1, B=1, K=1 the expected value is 5/2. The inverse of 2 modulo 1,000,000,007 is 500000004, so print the remainder of 5×500000004 divided by 1,000,000,007, which is 500000006.