Cuber QQ always envies those Kejin players, who pay a lot of RMB to get a higher level in the game. So he worked so hard that you are now the game designer of this game. He decided to annoy these Kejin players a little bit, and give them the lesson that RMB does not always work.
This game follows a traditional Kejin rule of “when you are level ii, you have to pay aiai RMB to get to level i+1i+1”. Cuber QQ now changed it a little bit: “when you are level ii, you pay aiai RMB, are you get to level i+1i+1 with probability pipi; otherwise you will turn into level xixi (xi≤ixi≤i)”.
Cuber QQ still needs to know how much money expected the Kejin players needs to ``ke’’ so that they can upgrade from level ll to level rr, because you worry if this is too high, these players might just quit and never return again.
Input
The first line of the input is an integer tt, denoting the number of test cases.
For each test case, there is two space-separated integers nn (1≤n≤500 0001≤n≤500 000) and qq (1≤q≤500 0001≤q≤500 000) in the first line, meaning the total number of levels and the number of queries.
Then follows nn lines, each containing integers riri, sisi, xixi, aiai (1≤ri≤si≤1091≤ri≤si≤109, 1≤xi≤i1≤xi≤i, 0≤ai≤1090≤ai≤109), space separated. Note that pipi is given in the form of a fraction risirisi.
The next qq lines are qq queries. Each of these queries are two space-separated integers ll and rr (1≤l The sum of nn and sum of qq from all tt test cases both does not exceed 106106. dp[i]表示从i-1级升到i级的花费期望 考虑从i级升到i+1的期望 有pi的概率升到i+1级,那么也就是说平均需要升级1/pi次才能升到i+1级(就是升级成功所需次数的期望),其中只有一次是成功的,其他都是失败的,那么就需要从xi级再升到i级,每次的花费是E[i]-E[xi],一共1/pi-1次 所以dp[i+1]=1/pi * ai+(1/pi-1)*(E[i]-E[xi]) 再把pi=ri/si 代进去,该求逆元求逆元(因为取模了) 同时更新 E[i+1]=E[i]+dp[i+1]
Output
For each query, output answer in the fraction form modulo 109+7109+7, that is, if the answer is PQPQ, you should output P⋅Q−1P⋅Q−1 modulo 109+7109+7, where Q−1Q−1 denotes the multiplicative inverse of QQ modulo 109+7109+7.
Sample Input
1
3 2
1 1 1 2
1 2 1 3
1 3 3 4
1 4
3 4
Sample Output
22
12题意:给你升级到A级的概率R/S和花费A若升级失败回到X级有Q次询问升级所需最小花费
思路
E[i]表示从1级升到i级的花费期望#include