#P4457. [BJOI2018] 治疗之雨

    ID: 3392 远端评测题 4000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>递推2018各省省选北京O2优化期望高斯消元

[BJOI2018] 治疗之雨

Description

Problem update: Since many people reported difficulty understanding the statement, but to respect the original wording we will not make large changes. You may interpret “minimum” and “maximum” as lower bound and upper bound, similar to the Health in the background.

You now have m+1m+1 numbers: the first is pp, with minimum 00 and maximum nn; the remaining mm are all infinite, with no minimum or maximum. You may perform any number of rounds. In each round, do the following:

  • Uniformly at random choose one number among those that are not at their maximum (if none, do nothing), and add 11 to it.
  • Then perform this step kk times: uniformly at random choose one number among those that are not at their minimum (if none, do nothing), and subtract 11 from it.

What is the expected number of rounds until the first number becomes its minimum 00.

Input Format

The input contains multiple test cases. The first line contains a positive integer TT, the number of test cases. The next TT lines each contain four non-negative integers nn, pp, mm, kk (as defined above), representing one query.

Output Format

Output TT lines, each with one integer, the answer for that query.

If the first number never becomes the minimum 00 no matter how many rounds, output -1.

Otherwise, it can be proved that the answer is a rational number. Output the answer modulo 109+710^9+7, i.e., suppose the answer is ab\frac{a}{b} (where aa and bb are coprime positive integers). You must output an integer xx such that 0x<109+70 \le x < 10^9+7 and abx(mod109+7)a \equiv b x \pmod{10^9+7}.

2
2 1 1 1
2 2 1 1
6
8

Hint

Constraints

  • For 10%10\% of the testdata, n3n \le 3, m,k2m, k \le 2.
  • For 20%20\% of the testdata, n,m,k5n, m, k \le 5.
  • For 30%30\% of the testdata, n,m,k30n, m, k \le 30.
  • For 40%40\% of the testdata, n,m,k50n, m, k \le 50.
  • For 50%50\% of the testdata, n,m,k200n, m, k \le 200.
  • For 70%70\% of the testdata, n200n \le 200.
  • For 80%80\% of the testdata, n500n \le 500.
  • For 100%100\% of the testdata, 1T1001 \le T \le 100, 1pn15001 \le p \le n \le 1500, 0m,k1090 \le m, k \le 10^9.

It is guaranteed that the case n=p=k=1n = p = k = 1, m=0m = 0 does not exist (because the author judged it wrong).

It is guaranteed that the denominator of the answer is not a multiple of 109+710^9+7 (because the author did not think of it).

Translated by ChatGPT 5