FZU - 2303 Mind control (组合数学推公式)

原题地址:http://acm.fzu.edu.cn/problem.php?pid=2303
题意:有N个人,N-1个人都有一个追随者且追随者关系为链状(即为一条长度为N的链),有M个蛋糕,随机的分配给M个人(每个人最多只能得到1个),分配给某个人就能得到这个人以及其追随者的信任,问信任的期望值为多少,假设结果为 x/y x / y 的形式,输出为 xy1yy1 x ∗ y − 1 , y ∗ y − 1 ,即 y1 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值