• loj #6216. 雪花挂饰


    (今天碰到的题怎么这么小清新

    $n$ 个不相同的点,$q$ 组询问,每次给定 $l,r$,问在 $n$ 个点中,选出 $x$ 个点 $(x in [l,r])$,用边连起来,能构成多少种不同的树

    $n,q leq 10^6$

    sol:

    首先知道 $n$ 个点的树有 $n^{n-2}$ 个,因为这题标号不同就算不同,所以 $i$ 个点不同的树有 $C_n^i imes i^{i-2}$

    维护一下这东西的前缀和就可以每组询问 $O(1)$ 了

    #include <bits/stdc++.h>
    #define LL long long
    using namespace std;
    #define rep(i, s, t) for (register int i = (s), i##end = (t); i <= i##end; ++i)
    #define dwn(i, s, t) for (register int i = (s), i##end = (t); i >= i##end; --i)
    inline int read() {
        int x = 0, f = 1;
        char ch = getchar();
        for (; !isdigit(ch); ch = getchar())
            if (ch == '-')
                f = -f;
        for (; isdigit(ch); ch = getchar()) x = 10 * x + ch - '0';
        return x * f;
    }
    const int maxn = 1e6 + 10;
    int n, T, mod, num[maxn], fac[maxn], ifac[maxn], cn[maxn], sum[maxn];
    inline int ksm(int x, int t) {
        if (t < 0)
            return 0;
        if (t == 0)
            return 1;
        int res = 1;
        for (; t; x = 1LL * x * x % mod, t = t >> 1)
            if (t & 1)
                res = 1LL * x * res % mod;
        return res;
    }
    int main() {
        n = read(), T = read(), mod = read();
        rep(i, 1, n) num[i] = ksm(i, i - 2);
        ifac[0] = fac[0] = 1;
        rep(i, 1, n) fac[i] = 1LL * fac[i - 1] * i % mod;
        ifac[n] = ksm(fac[n], mod - 2);
        dwn(i, n - 1, 1) ifac[i] = 1LL * ifac[i + 1] * (i + 1) % mod;
        rep(i, 1, n) cn[i] = 1LL * (1LL * fac[n] * ifac[n - i] % mod) * ifac[i] % mod;
        rep(i, 1, n) sum[i] = (sum[i - 1] + (1LL * cn[i] * num[i] % mod)) % mod;
        while (T--) {
            int l = read(), r = read();
            int ans = (((sum[r] - sum[l - 1]) % mod) + mod) % mod;
            printf("%d
    ", ans);
        }
    }
    View Code
  • 相关阅读:
    静态链接到 MFC 的规则 DLL
    DLLAFX_MANAGE_STATE(AfxGetStaticModuleState())模块状态切换保护
    多线程专题之线程同步(1)
    UVa 4256 Salesmen dp
    nefu 661 Clockwise 水DP+略几何
    10635 Prince and Princess LCS&LIS
    UVa 3882 And Then There Was One 递推无力orz
    UVa 10192 Vacation 字符串dp
    UVa 11584 Partitioning by Palindromes 回文串dp
    UVa 10534 Wavio Sequence LIS
  • 原文地址:https://www.cnblogs.com/Kong-Ruo/p/10491084.html
Copyright © 2020-2023  润新知