原题地址:http://acm.fzu.edu.cn/problem.php?pid=2303
题意:有N个人,N-1个人都有一个追随者且追随者关系为链状(即为一条长度为N的链),有M个蛋糕,随机的分配给M个人(每个人最多只能得到1个),分配给某个人就能得到这个人以及其追随者的信任,问信任的期望值为多少,假设结果为
x/y
x
/
y
的形式,输出为
x∗y−1,y∗y−1
x
∗
y
−
1
,
y
∗
y
−
1
,即
y−1
y
−
1
为y相对于
(1e9+7)
(
1
e
9
+
7
)
的逆元。(T组数据 ,T,N,M<=1e6)
思路:具体推公式见这篇博客;
#include <cmath>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <cctype>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)+1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
ll Pow(ll base, ll n) {
ll res = 1;
while (n) {
if (n & 1) res = res * base % mod;
n >>= 1;
base = base * base % mod;
}
return res;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
ll n, m;
scanf("%I64d%I64d", &n, &m);
if (m > n) printf("%I64d\n", n % mod);
else {
ll a = m * (n + 1)%mod;
ll b = m + 1;
printf("%I64d\n", (a * Pow(b, mod - 2) % mod));
}
}
return 0;
}