There is a wonderful country where people like eating very much. Each person has exactly one direct follower while the follower’s follower is also follower of this man (of course the most unattractive man doesn’t have his follower).
And you find some amazing cake, if you give it to a person, you can have the trust of this person and all his followers.
Now you want to give out M cakes randomly to N person (each person can get at most one cake), please calculate the expectation of the trust you have earned.
Because the answer can write as a / b, please output a * b^-1 mod 10^9 + 7. (b * b^-1 ≡ 1 mod 10^9 + 7)
Input
First line a single integer T indicating the number of cases. T<=1000000;
For each case, first line two number N and M described before. N, M<=1000000;
Output
A single integer indicating the expectation of the trust you have earned. (mod 1e9 + 7)
Sample Input
2
3 3
3 2
Sample Output
3
666666674
题意:从n个人中选m个,得分为这m个人中最大的编号,求随即选择的得分的数学期望。
- 个人思路:
按照题意,我们先限定最高的编号,然后在这个编号下的编号中选出m-1个数。逐一选择每个最高编号,可以发现这种取法可以涵盖所有取法可能,猜想得到下面结论:
画杨辉三角验证发现成立:
按照题意,求数学期望,逐步化简,中间再次套用①公式,得到o(1)的结论:
得到结论后求个逆元就可以了。
#include<iostream>
#define rep(i,n) for(int i=1;i<=n;i++)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll n,m;
ll qpow(ll x,ll k) {
if(k==0) return 1;
ll t=qpow(x,k/2);
if(k&1) return t*t%mod*x%mod;
else return t*t%mod;
}
void solve(ll ans=0) {
cin>>n>>m;
cout<<(n+1)*m%mod*qpow(m+1,mod-2)%mod<<endl;
}
int main() {
ios::sync_with_stdio(false);
int T;cin>>T;
while(T--) solve();
}