loj #6261 一个人的高三楼 FFT + 组合数递推

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

一天的学习快要结束了,高三楼在晚自习的时候恢复了宁静。
不过,\(HSD\) 桑还有一些作业没有完成,他需要在这个晚自习写完。比如这道数学题:

1、给出一个数列,求它的前 \(i\) 项和 \(S_i\)\(i\in \{x|1\le x\le n,x\in \mathbb{N}\}\)

HSD 桑擅长数学,很快就把这题秒了……
然而还有第二题:

2、如果把上一问的前 \(i\) 项和看成一个新数列,请求出它的前 \(i\) 项和

看完第二题,还有第三题……HSD 桑已经预感到情况不妙了。
HSD 桑大致看了看题,发现有些规律。其实就是在求 \(k\) 次前缀和。如果我们借用函数迭代的标记,就是在求 \(S_n^{(k)}\)……
HSD 桑还有很多作业要写,请你帮助他完成这项作业。

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

第一行,两个正整数 \(n,k\)\(n\) 表示数列的长度,\(k\) 的意义如题目描述; 第二行,\(n\) 个正整数,表示这个数列,两个数之间用一个空格隔开。

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

\(n\) 行,每行一个数,第 \(i\) 行表示 \(S_i^{(k)}\),结果可能会非常大,请对 \(998244353\) 取模后输出。

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

4 1
1 2 3 4
    
4 3
1 2 3 4

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

1
3
6
10
    
1
5
15
35

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

样例解释 1

对于这个序列,求它的 11 次前缀和,就是输出这个数列的前缀和咯……

样例解释 2

要求这个数列的 33 次前缀和,这个数列的 11 次前缀和为 {1,3,6,10}{1,3,6,10},22 次前缀和为 {1,4,10,20}{1,4,10,20},33 次前缀和即为 {1,5,15,35}{1,5,15,35}。

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

天真的我一看,快速幂套FFT,傻逼题啊,于是开始切

然后。。。。各种TLE

然后发现,根组合数有关

比如

a b c d

a a+b a+b+c a+b+c+d

a 2a+b 3a+2b+c 4a+3b+2c+d

a 3a+b 6a+3b+c 10a+6b+3c+d

这。。。

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

这TM前面的系数居然TM是组合数的一列

但是尼玛k是\(2^{60}\)啊,怎么求组合数???

这时gw提醒了我,第一项就是1啊,然后后面随便成一下\(O(n)\)递推就行了啊

恍然大悟自己多么愚蠢qwq

刚好是倒过来的,所以不用翻转,直接FFT出ans

#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;
}
using std::vector;
const int mod = 998244353;
const int maxn = 4e6 + 100;
int len, r[maxn];
LL c;
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 w = 1, a0 = i, a1 = i + l;
            for(int k = 0; k < l; k++, a0++, a1++, w = 1LL * w * w0 % mod) {
                int tmp = 1LL * A[a1] * w % mod;
                A[a1] = ((A[a0] - tmp) % mod + mod) % mod;
                A[a0] = (A[a0] + tmp) % mod;
            }
        }
    }
    if(!(~flag)) {
        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));
    FNTT(A, 1), FNTT(B, 1);
    vector<int> ans;
    ans.resize(len);
    for(int i = 0; i < len; i++) ans[i] = 1LL * A[i] * B[i] % mod;
    FNTT(ans, -1);
    ans.resize(tot);
    return ans;
}
int main() {
    int n = in();
    c = in() % mod;
    vector<int> a, b;
    for(int i = 1; i <= n; i++) a.push_back(in());
    LL now = 1;
    for(int i = 1; i <= n; i++) {
        b.push_back((int)(now % mod));
        now = (1LL * now * (c + i - 1) % mod) * ksm(i, mod - 2) % mod;
    }
    a = a * b;
    for(int i = 0; i < n; i++) printf("%d\n", a[i]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/olinr/p/10283895.html