2019.2.10考试T2, 多项式求exp+生成函数

\(\color{#0066ff}{ 题目描述 }\)

为了减小文件大小,这里不写一堆题目背景了。

请写一个程序,输入一个数字N,输出N个点的森林的数量。点有标号。

森林是一种无向图,要求图中不能存在环(图可以不连通),或者说是由若干个树组成的集合。说到森林,我就想起今年下半年,中美合拍的西游记即将正式开机,我继续扮演美猴王孙悟空,我会用美猴王艺术形象努力创造一个正能量的形象,文体两开花,弘扬中华文化,希望大家能多多关注。

\(\color{#0066ff}{输入格式}\)

输入文件只有一个整数N。

\(\color{#0066ff}{输出格式}\)

输出森林的方案数。由于答案很大,所以请输出对998244353取模后的结果。

\(\color{#0066ff}{输入样例}\)

3

\(\color{#0066ff}{输出样例}\)

7

\(\color{#0066ff}{数据范围与提示}\)

对于30%的数据满足3<=n<=9。

对于40%的数据满足3<=n<=90。

对于50%的数据3<=n<=900。

对于60%的数据满足3<=n<=9000。

对于100%的数据满足3<=n<=90000。

\(\color{#0066ff}{ 题解 }\)

规律总结:对于有标号类问题,个体的exp就是集合,集合的ln就是个体

本题来说,个体是个结论,即一棵树的有标号的种类为\(n^{n-2}\)

于是\(ans=\sum\frac{i^{i-2}}{i!}x^i\)

前面的根据公式可以化为exp,最后再乘上n!即可

#include<bits/stdc++.h>
#define LL long long
LL in() {
    char ch; LL x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 4e6 + 100;
const int mod = 998244353;
using std::vector;
int len, r[maxn], fac[maxn];
LL ksm(LL x, LL y) {
    LL re = 1LL;
    while(y) {
        if(y & 1) re = re * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return re;
}
void FNTT(vector<int> &A, int flag) {
    A.resize(len);
    for(int i = 0; i < len; i++) if(i < r[i]) std::swap(A[i], A[r[i]]);
    for(int l = 1; l < len; l <<= 1) {
        int w0 = ksm(3, (mod - 1) / (l << 1));
        for(int i = 0; i < len; i += (l << 1)) {
            int a0 = i, a1 = i + l, w = 1;
            for(int k = 0; k < l; k++, a0++, a1++, w = 1LL * w0 * w % mod) {
                int tmp = 1LL * A[a1] * w % mod;
                A[a1] = ((A[a0] - tmp) % mod + mod) % mod;
                A[a0] = (A[a0] + tmp) % mod;
            }
        }
    }
    if(flag == -1) {
        std::reverse(A.begin() + 1, A.end());
        int inv = ksm(len, mod - 2);
        for(int i = 0; i < len; i++) A[i] = 1LL * A[i] * inv % mod;
    }
}
vector<int> operator * (vector<int> A, vector<int> B) {
    int tot = A.size() + B.size() - 1;
    for(len = 1; len <= tot; len <<= 1);
    for(int i = 0; i < len; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) * (len >> 1));
    vector<int> ans;
    FNTT(A, 1), FNTT(B, 1);
    for(int i = 0; i < len; i++) ans.push_back(1LL * A[i] * B[i] % mod);
    FNTT(ans, -1);
    ans.resize(tot);
    return ans;
}
vector<int> operator - (const vector<int> &A, const vector<int> &B) {
    vector<int> ans;
    for(int i = 0; i < (int)std::min(A.size(), B.size()); i++) ans.push_back(A[i] - B[i]);
    for(int i = A.size(); i < (int)B.size(); i++) ans.push_back(-B[i]);
    for(int i = B.size(); i < (int)A.size(); i++) ans.push_back(A[i]);
    return ans;
}
vector<int> operator + (const vector<int> &A, const vector<int> &B) {
    vector<int> ans;
    for(int i = 0; i < (int)std::min(A.size(), B.size()); i++) ans.push_back(A[i] + B[i]);
    for(int i = A.size(); i < (int)B.size(); i++) ans.push_back(B[i]);
    for(int i = B.size(); i < (int)A.size(); i++) ans.push_back(A[i]);
    return ans;
}


vector<int> inv(const vector<int> &A) {
    if(A.size() == 1) {
        vector<int> ans;
        ans.push_back(ksm(A[0], mod - 2));
        return ans;
    }
    int n = A.size(), _ = (n + 1) >> 1;
    vector<int> B = A, ans;
    ans.push_back(2);
    B.resize(_);
    B = inv(B);
    ans = B * (ans - A * B);
    ans.resize(n);
    return ans;
}
vector<int> getd(const vector<int> &A) {
    vector<int> ans;
    ans.resize(A.size() - 1);
    for(int i = 1; i < (int)A.size(); i++) ans[i - 1] = 1LL * A[i] * i % mod;
    return ans;
}
vector<int> geti(const vector<int> &A) {
    vector<int> ans;
    ans.resize(A.size() + 1);
    for(int i = 0; i < (int)A.size(); i++) ans[i + 1] = 1LL * A[i] * ksm(i + 1, mod - 2) % mod;
    return ans;
}

vector<int> getln(const vector<int> &A) { return geti(getd(A) * inv(A)); }
vector<int> getexp(const vector<int> &A) {
    if(A.size() == 1) {
        vector<int> ans;
        ans.push_back(1);
        return ans;
    }
    int n = A.size(), _ = (n + 1) >> 1;
    vector<int> B = A, ans;
    ans.push_back(1);
    B.resize(_);
    B = getexp(B);
    ans = B * (ans - getln(B) + A);
    ans.resize(n);
    return ans;
}
    
int main() {
	freopen("forest.in", "r", stdin);
	freopen("forest.out", "w", stdout);
	int n = in() + 1;
    vector<int> a;
	fac[0] = 1;
	for(LL i = 1; i <= n; i++) fac[i] = 1LL * fac[i - 1] * i % mod;
	a.push_back(0);
	a.push_back(1);
    for(int i = 2; i <= n; i++) a.push_back(1LL * ksm(i, i - 2) * ksm(fac[i], mod - 2) % mod);
    a.resize(a.size() << 1);
    a = getexp(a);
	printf("%lld",  1LL * a[n - 1] * fac[n - 1] % mod);
    return 0;
}
posted @ 2019-02-10 19:05  olinr  阅读(233)  评论(0编辑  收藏  举报