题解 P5219 【无聊的水题 I】

· · 题解

这是一篇民科的直接从生成函数角度进行推导的题解。

首先,根据 Prufer 序列,易知相当于计数长度为 n-2 的,元素在 [1,n] 内且所有元素出现次数的最大值恰为 m-1 的序列个数。
但是最大值恰为 m-1 并不好求,考虑差分转化为出现次数全都不超过 m-1 的,减去出现次数全都不超过 m-2 的。

于是考虑如何计数长度为 n-2,元素出现次数不超过 m-1 的序列个数。
序列当然是用 EGF,因为要考虑顺序。由于每种元素的出现次数都不能超过 m-1,所以可以考虑对每种元素构造 EGF。
则设

F(x) = \sum\limits_{i=0}^{m-1} \frac{x^i}{i!}

因为同元素之间换顺序也只算一种方案。

由于有 [1,n] 的元素,所以答案就是

(n-2)![x^{n-2}]F^n(x)

多项式倍增快速幂或者 ln exp 快速幂解决。
复杂度 O(n \log^2 n) 或 O(n \log n)。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <algorithm>
#define add(a,b) (a + b >= mod ? a + b - mod : a + b)
#define dec(a,b) (a < b ? a - b + mod : a - b)
using namespace std;
const int N = 1 << 18;
const int mod = 998244353;
const int G = 3;
inline int fpow(int a,int b)
{
    int ret = 1;
    for(;b;b >>= 1)
        (b & 1) && (ret = (long long)ret * a % mod),a = (long long)a * a % mod;
    return ret;
}
struct poly
{
    int a[N + 5];
    inline const int &operator[](int x) const
    {
        return a[x];
    }
    inline int &operator[](int x)
    {
        return a[x];
    }
    inline void clear(int x = 0)
    {
        memset(a + x,0,(N - x + 1) << 2);
    }
} f;
int m,len,k,n,lg2[N + 5];
int cnt[N + 5];
int rev[N + 5],fac[N + 5],ifac[N + 5],inv[N + 5];
int rt[N + 5],irt[N + 5];
inline void init(int len)
{
    for(n = 1;n < len;n <<= 1);
    for(register int i = 2;i <= n;++i)
        lg2[i] = lg2[i >> 1] + 1;
    int w = fpow(G,(mod - 1) / n);
    rt[n >> 1] = 1;
    for(register int i = (n >> 1) + 1;i <= n;++i)
        rt[i] = (long long)rt[i - 1] * w % mod;
    for(register int i = (n >> 1) - 1;i;--i)
        rt[i] = rt[i << 1];
    fac[0] = 1;
    for(register int i = 1;i <= n;++i)
        fac[i] = (long long)fac[i - 1] * i % mod;
    ifac[n] = fpow(fac[n],mod - 2);
    for(register int i = n;i;--i)
        ifac[i - 1] = (long long)ifac[i] * i % mod;
    for(register int i = 1;i <= n;++i)
        inv[i] = (long long)ifac[i] * fac[i - 1] % mod;
}
inline void ntt(poly &a,int type,int n)
{
    type == -1 && (reverse(a.a + 1,a.a + n),1);
    int lg = lg2[n] - 1;
    for(register int i = 0;i < n;++i)
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << lg),
        i < rev[i] && (swap(a[i],a[rev[i]]),1);
    for(register int w = 2,m = 1;w <= n;w <<= 1,m <<= 1)
        for(register int i = 0;i < n;i += w)
            for(register int j = 0;j < m;++j)
            {
                int t = (long long)rt[m | j] * a[i | j | m] % mod;
                a[i | j | m] = dec(a[i | j],t),a[i | j] = add(a[i | j],t);
            }
    if(type == -1)
        for(register int i = 0;i < n;++i)
            a[i] = (long long)a[i] * inv[n] % mod;
}
inline void mul(poly &a,const poly &b,int n)
{
    static poly x,y;
    int lim = 1;
    x.clear(),y.clear();
    for(;lim < (n << 1);lim <<= 1);
    x = a,y = b;
    x.clear(n),y.clear(n);
    ntt(x,1,lim),ntt(y,1,lim);
    for(register int i = 0;i < lim;++i)
        x[i] = (long long)x[i] * y[i] % mod;
    ntt(x,-1,lim);
    x.clear(n),a = x;
}
inline poly inverse(const poly &f,int n)
{
    static int s[30];
    static poly g,h,q;
    int lim = 1,top = 0;
    g.clear();
    for(;n > 1;s[++top] = n,n = (n + 1) >> 1);
    g[0] = fpow(f[0],mod - 2);
    for(;top;--top)
    {
        n = s[top];
        for(;lim < (n << 1);lim <<= 1);
        q = g,h = f,h.clear(n);
        ntt(g,1,lim),ntt(h,1,lim);
        for(register int i = 0;i < lim;++i)
            g[i] = (long long)g[i] * g[i] % mod * h[i] % mod;
        ntt(g,-1,lim);
        for(register int i = 0;i < n;++i)
            g[i] = dec(add(q[i],q[i]),g[i]);
        g.clear(n);
    }
    return g;
}
inline void derivative(poly &f,int n)
{
    for(register int i = 1;i < n;++i)
        f[i - 1] = (long long)f[i] * i % mod;
    f[n - 1] = 0;
}
inline void integral(poly &f,int n)
{
    for(register int i = n - 1;~i;--i)
        f[i + 1] = (long long)f[i] * inv[i + 1] % mod;
    f[0] = 0;
}
inline poly ln(const poly &f,int n)
{
    static poly g;
    g = f,derivative(g,n),mul(g,inverse(f,n),n),integral(g,n);
    return g;
}
inline poly exp(const poly &f,int n)
{
    static int s[30];
    static poly g,h;
    int lim = 1,top = 0;
    g.clear();
    for(;n > 1;s[++top] = n,n = (n + 1) >> 1);
    g[0] = 1;
    for(;top;--top)
    {
        n = s[top];
        for(;lim < (n << 1);lim <<= 1);
        h = g,g = ln(g,n);
        for(register int i = 0;i < n;++i)
            g[i] = dec(f[i],g[i]);
        g[0] = add(g[0],1);
        ntt(g,1,lim),ntt(h,1,lim);
        for(register int i = 0;i < lim;++i) 
            g[i] = (long long)g[i] * h[i] % mod;
        ntt(g,-1,lim);
        g.clear(n);
    }
    return g;
}
inline poly power(const poly &f,int k,int n)
{
    static poly g;
    g = ln(f,n);
    for(register int i = 0;i < n;++i)
        g[i] = (long long)g[i] * k % mod;
    g = exp(g,n);
    return g;
}
namespace Mod_sqrt
{
    typedef pair<int,int> cp;
    int w;
    inline cp operator*(const cp &a,const cp &b)
    {
        return cp(((long long)a.first * b.first % mod + (long long)a.second * b.second % mod * w % mod) % mod,((long long)a.first * b.second % mod + (long long)a.second * b.first % mod) % mod);
    }
    inline cp pow(cp a,int b)
    {
        cp ret(1,0);
        for(;b;b >>= 1)
            (b & 1) && (ret = ret * a,1),a = a * a;
        return ret;
    }
    inline int mod_sqrt(int x)
    {
        int y = rand() % mod;
        for(;fpow(w = ((long long)y * y % mod - x + mod) % mod,mod - 1 >> 1) <= 1;y = rand() % mod);
        cp ret = pow(cp(y,1),mod + 1 >> 1);
        return min(ret.first,mod - ret.first);
    }
}
using Mod_sqrt::mod_sqrt;
inline poly sqrt(const poly &f,int n)
{
    static int s[30];
    static poly g,h;
    int top = 0;
    g.clear();
    for(;n > 1;s[++top] = n,n = (n + 1) >> 1);
    g[0] = mod_sqrt(f[0]);
    for(;top;--top)
    {
        n = s[top];
        for(register int i = 0;i < n;++i)
            h[i] = add(g[i],g[i]);
        h = inverse(h,n),mul(g,g,n);
        for(register int i = 0;i < n;++i)
            g[i] = add(g[i],f[i]);
        mul(g,h,n);
    }
    return g;
}
inline int calc(int n,int m)
{
    if(m <= 0)
        return 0;
    static poly f;
    for(register int i = 0;i <= m;++i)
        f[i] = ifac[i];
    f.clear(m + 1),f = power(f,n,n - 1);
    return (long long)f[n - 2] * fac[n - 2] % mod;
}
int main()
{
    scanf("%d%d",&len,&m),init((len - 1) << 1);
    printf("%d\n",(calc(len,m - 1) - calc(len,m - 2) + mod) % mod);
}