• 牛客OI周赛4-提高组 B 最后的晚餐(dinner)


    最后的晚餐(dinner)

    思路:

    容斥

    求 ∑(-1)^i * C(n, i) * 2^i * (2n-i-1)!

    这道题卡常数

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
    //head
    
    const LL MOD = 1e9 + 7;
    const int N = 3e7 + 5;
    int f[N], inv[N];
    LL q_pow(LL n, LL k) {
        LL ans = 1;
        while(k) {
            if(k&1) ans = (ans * n) % MOD;
            n = (n * n) % MOD;
            k >>= 1;
        }
        return ans;
    }
    int main() {
        int n;
        scanf("%d", &n);
        if(n == 1) return 0*puts("0");
        LL ans = 0;
        int fac = 1, pw = q_pow(2, n), c = 1;
        f[0] = 1;
        for (int i = 1; i <= n; i++) f[i] = (1LL * f[i-1] * i) % MOD;
        inv[n] = q_pow(f[n], MOD-2) % MOD;
        for (int i = n-1; i >= 0; i--) inv[i] = (1LL * inv[i+1]*(i+1)) % MOD;
        fac = f[n-1];
        for (int i = n; i >= 0; i--) {
            (ans += ((i%2) ? -1 : 1) * 1LL * inv[i] % MOD * inv[n-i] % MOD * pw % MOD * fac) %= MOD;
            fac = (1LL * fac * (2*n - i)) % MOD;
            pw = (1LL * pw * inv[2]) % MOD;
        }
        ans = (ans + MOD) % MOD;
        ans = ans * f[n] % MOD;
        printf("%lld
    ", ans);
        return 0;
    }
  • 相关阅读:
    php排序算法-冒泡排序
    Mac安装java JDK
    mysql索引简单记录一下
    Mac 通过 pecl安装 redis 扩展
    Mac通过pecl安装swool时遇到的坑(root + openssl)
    php获取两个日期的之间的日期信息,返回数组
    2021.4.9训练
    王道数据结构代码:双向链表的操作
    王道数据结构代码:单链表的操作
    PTA 7-1 大炮打蚊子 (15 分)
  • 原文地址:https://www.cnblogs.com/widsom/p/9877175.html
Copyright © 2020-2023  润新知