Problem 3051 --Rikka with Quicksort

3051: Rikka with Quicksort

"
Time Limit $3$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $1$ 正确数量 $1$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签
Rikka is interested in computer science, and she has been practicing coding for two years and a half. Today, she wants to do a simple summary of the algorithms she has learned. 

One of the most important algorithms is Quicksort. Though its idea is quite simple, Rikka remembers that it took her a while to prove the time complexity. Let f(n) be the expected number of comparisons required by Quicksort on a sequence with length n. Then f(n) follows the following equations:
f(0)f(i)=0=i−1+1i∑j=1i(f(j−1)+f(i−j))i≥1

After some simple derivations, Rikka finishes the proof and obtains the result that f(n)=O(nlogn): As an outstanding undergraduate student, this problem is just a piece of cake for her.

To make the task more challenging, Rikka asks Yuta, her boyfriend, to set several exercises for her. The following is the hardest one of them:

Consider a modified version of Quicksort: the recursive process terminates once the length of the interval is less than m. At this time, the expected number of comparisons gm(n) can be described with the following equations:
gm(i)gm(i)=0=i−1+1i∑j=1i(gm(j−1)+gm(i−j))0≤i≤mi>m

Now, Yuta shows the value of n,m, and he wants Rikka to calculate gm(n). It is generally known that Rikka is not good at math. Therefore she wants you to help her calculate the answer.
The first line is an integer t(1≤t≤10), the number of test cases.

For each test case, the input contains a single line with two positive integers n,m(1≤m≤n≤109).
For each test case, output a single line with a single number, the value of gm(n)

Clearly, gm(n) is a rational number. Therefore, you are required to print gm(n) mod 1000000007, i.e., print x×y−1 mod 1000000007 where xy is the irreducible fraction representation of gm(n).
5
3 1
5 1
5 3
10 5
1000 500
666666674
800000013
400000008
308730177
3107840

推荐代码 查看3051 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$1307 $ms] 淡意的温柔 588054 2020-05-27 09:42:19
内存最少[$2100 $KB] 淡意的温柔 588054 2020-05-27 09:42:19
第一AC 淡意的温柔 588054 2020-05-27 09:42:19
第一挑战 淡意的温柔 588054 2020-05-27 09:42:19

赛题来源/所属竞赛 2019 Multi-University Training Contest 9 N/A

竞赛编号 竞赛名称 竞赛时间 访问比赛