首页 > 其他分享 >gym-103708E Erudite of words

gym-103708E Erudite of words

时间:2022-08-28 22:58:03浏览次数:101  
标签:invfac int gym maxn words ans 103708E ll mod

Erudite of words

组合数学 + 容斥

定义 \(F_i\):表示由 \(i\) 个字母组成的长度为 \(n\) 的单词数(每个字母必须在单词中出现)

显然答案就是 \(F_k * C_{m}^{k}\)

关于 \(F_i\) 的递推式:

\[F_i = i^n - \sum_{j=1}^{k-1}(F_j) \]

显然 \(i^n\) 代表 \(i\) 个字母随意摆放的情况,容斥地减去字母数为 \(1,2,3,...,i-1\) 的情况后,剩下的就是 \(F_i\)

大量计算组合数的时候,可以用递推预处理阶乘和阶乘逆元,询问组合数的时候就是 \(O(1)\)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
const ll mod = 1e9 + 7;
ll fac[maxn], last[maxn], invfac[maxn];

ll qpow(ll x, ll n)
{
    ll ans = 1;
    while(n)
    {
        if(n & 1) ans = x * ans % mod;
        n >>= 1;
        x = x * x % mod;
    }
    return ans % mod;
}

ll cal(ll up, ll down)
{
    ll ans = fac[down] * invfac[up] % mod;
    ans = ans * invfac[down - up] % mod;
    return ans;
}

int main()
{
    ll n, m, k;
    cin >> n >> m >> k;
    fac[0] = 1;
    for(int i=1; i<maxn; i++) fac[i] = fac[i-1] * i % mod;
    invfac[maxn - 1] = qpow(fac[maxn - 1], mod - 2);
    for(int i=maxn-2; i>=1; i--)
        invfac[i] = invfac[i + 1] * (i + 1) % mod;
    invfac[0] = invfac[1];
    if(k > n) {cout << 0 << endl; return 0;}
    for(int i=1; i<=k; i++)
    {
        last[i] = qpow(i, n);
        ll ans = 0;
        for(int j=1; j<i; j++)
            ans = (ans + cal(j, i) * last[j]) % mod;
        last[i] = (last[i] - ans) % mod + mod;
        last[i] %= mod;
    }
    cout << cal(k, m) * last[k] % mod << endl;
    return 0;
}

标签:invfac,int,gym,maxn,words,ans,103708E,ll,mod
From: https://www.cnblogs.com/dgsvygd/p/16634345.html

相关文章

  • gym-103708D Different Pass a Ports
    DifferentPassaPorts矩阵快速幂模板图的邻接矩阵的\(k\)次幂就是从图上所有点走\(k\)步的方案数#include<iostream>#include<cstdio>usingnamespacestd;......
  • gym-103708F Froginald the frog
    Froginaldthefrog矩阵快速幂如果没有分隔的话,这就是一个矩阵快速幂求斐波那契的问题因为有分隔,因此考虑他们分成若干个块,每个块的方案数之积就是答案,显然分隔的长度如......
  • gym-103708B Building 5G antennas
    Building5Gantennasdfs剪枝要字典序最小,显然第一个点就是\(1\),后面考虑走\(k\)步后能到达的点集中选一个字典序最小的,重复该过程考虑\(set[i][j]\)表示第\(i\)......
  • 8.27训练赛(2018-2019, ICPC, Asia Yokohama Regional Contest 2018,gym102082)
    B一开始开题的时候想假了,以为用map存差的结果贪心就行了,实际上是一个比较妙的dp,用到了一个结论:两项就唯一确定一个等差数列。设\(f[i,j]\)表示最后两个数选了\(a_i\),\(a......
  • gym中的action_repeat
    Specifically,weaverageperformanceover10randomseeds,andreducethenumberoftrainingobservationsinverseproportionallytotheactionrepeatvalue.—......
  • 文本编辑器MarkMyWords
    MarkMyWords是一款提供实时预览格式化的文章或预览HTML代码等功能的文本编辑器,可以帮助大家在无干扰模式下进行文本编辑,提高大家工作时的专注力。MarkMyWords为大家提供了......
  • 804. Unique Morse Code Words
    InternationalMorseCodedefinesastandardencodingwhereeachletterismappedtoaseriesofdotsanddashes,asfollows:'a' mapsto ".-",'b' mapsto......
  • HDU - 2222 Keywords Search
    思路:AC自动机模板+拓扑优化。因为当前单词可能包含别的单词,所以需要一直nex,但是本题会超时,所以思考优化这个过程。我们知道,拓扑优化,可以将所有出现过的字符都标记上,这......
  • [Codeforces_gym_102136] I.Permutations again
    传送门DescriptionGivenasequence\(A_i\)consistingof\(N\)integers.Findthenumberofpairs\((L,R)\)forwhichthesubsegment\({A_L,A_{L + 1},......
  • Gym102798 CCPC2020威海E加强版 题解
    原题link把\(m\)和\(a_i\)的上界改成\(200\),其他不变.基本思路:枚举\(S\),求出\(p(S)\)表示集合\(S\)中的怪兽被打死的概率,答案就是\(\sum_{S}|S|p(S)\).而这......