【FZU-2303】Mind control

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();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章