其他分享
首页 > 其他分享> > Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学

Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学

作者:互联网

Sasha and Interesting Fact from Graph Theory

n 个 点形成 m 个有标号森林的方案数为 F(n, m) = m * n ^ {n - 1 - m}

然后就没啥难度了。。。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

int power(int a, int b) {
    int ans = 1;
    while(b) {
        if(b & 1) ans = 1LL * ans * a % mod;
        a = 1LL * a * a % mod; b >>= 1;
    }
    return ans;
}

int F[N], Finv[N], inv[N];
int C(int n, int m) {
    if(n < 0 || n < m) return 0;
    return 1LL * F[n] * Finv[m] % mod * Finv[n - m] % mod;
}

int n, m, a, b;

int main() {
    inv[1] = F[0] = Finv[0] = 1;
    for(int i = 2; i < N; i++) inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
    for(int i = 1; i < N; i++) F[i] = 1LL * F[i - 1] * i % mod;
    for(int i = 1; i < N; i++) Finv[i] = 1LL * Finv[i - 1] * inv[i] % mod;
    scanf("%d%d%d%d", &n, &m, &a, &b);
    int ans = 0;
    for(int i = 2; i <= n; i++) {
        if(i < n) add(ans, 1LL * C(n - 2, i - 2) * F[i - 2] % mod * C(m - 1, i - 2) % mod * power(m, n - i) % mod * i % mod * power(n, n - i - 1) % mod);
        else add(ans, 1LL * F[i - 2] * C(m - 1, i - 2) % mod);
    }
    printf("%d\n", ans);
    return 0;
}

/*
*/

 

标签:const,Theory,int,题解,Finv,1LL,Interesting,mod,define
来源: https://www.cnblogs.com/CJLHY/p/10749474.html