#P7108. 移花接木

    ID: 5898 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学贪心洛谷原创O2优化逆元洛谷月赛

移花接木

Description

The spirit tree’s original form can be viewed as a full aa-ary tree with height 10101010{10}^{{10}^{{10}^{10}}}. The height is defined as the number of edges from the root node to a leaf node.

Affected by the decayed power, the spirit tree can only maintain the form of a full bb-ary tree with height exactly hh. To transform into this form, the spirit tree has two kinds of magic:

  • Transplanting: choose an edge uvu \to v (uu is the parent of vv), remove this edge and the entire subtree rooted at vv.
  • Grafting: choose an edge uvu \to v (uu is the parent of vv) and a node ww (ww cannot be in the subtree of vv or a node that has been removed), and reattach the endpoint at uu of this edge to ww. This magic can only be used when the number of edges from the root node to u\boldsymbol{u} is 101010\le 10^{10^{10}}.

The spirit tree has limited accumulated magical power, so it must complete the transformation using the minimum number of spells. This is a long process: even the minimum number can be extremely large. You only need to compute the minimum number modulo 109+710^9 + 7.

Input Format

The first line contains the number of test cases TT.

The next TT lines each contain three integers a,b,ha, b, h, describing one test case.

Output Format

For each test case, output one integer per line, the answer modulo 109+710^9 + 7.

2
1 2 1
3 2 1

2
7

Hint

[Sample Explanation #1]

Below is the two-step transformation process for a=1a=1, b=2b=2, h=1h=1. The extremely tall redundant subtrees in the figure are replaced by ellipses.

It can be proven that the answer for this test case cannot be less than 22.


[Constraints and Notes]

This problem uses bundled tests. You must pass all test points in a Subtask to obtain the score for that Subtask.

  • Subtask #1 (3 points): h=0h = 0.
  • Subtask #2 (4 points): a=ba = b.
  • Subtask #3 (8 points): a=1a = 1.
  • Subtask #4 (8 points): b=1b = 1.
  • Subtask #5 (17 points): h10h \le 10.
  • Subtask #6 (15 points): h106h \le 10^6. For each test point, there exist a,b\overline{a}, \overline{b} such that the testdata satisfy a=aa=\overline{a} and b=bb=\overline{b}.
  • Subtask #7 (15 points): h106h \le 10^6.
  • Subtask #8 (30 points): no special restrictions.

All test points (except the samples) contain 10610^6 test cases, i.e. T=106T = 10^6. Be sure to use fast I/O methods.

For all test cases, it is guaranteed that 1a,b1091 \le a,b \le 10^9 and 0h1090 \le h \le 10^9.

Translated by ChatGPT 5