vector 用得很野蛮,不过反正有转置原理和 DIF-DIT 优化常数,跑得还是非常快的。
但是常数基本比其他转置原理求值都大
MultiEval() 是多点求值。
// #define FILEIO
#define DEBUG
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#define rep(a,b,c) for(int a = (b);a <= (c);++a)
#define repb(a,b,c) for(int a = (b);a >= (c);--a)
#define repl(a,b,c) for(int a = (b);a < (c);++a)
#define repr(a,b,c) for(int a = (b);a > (c);--a)
#define repg(a,b) for(int a = head[b];~a;a = e[a].nxt)
#define mems(a,b) memset(a,b,sizeof(a))
#define space PutC(32)
#define enter PutC(10)
#ifdef DEBUG
#define O(x) std::cerr << #x << ':' << x << std::endl;
#else
#define assert _ILLEGAL_
#define fprintf _ILLEGAL_
#endif
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using f32 = float;
using f64 = double;
using f80 = long double;
using pii = std::pair<int,int>;
using vec = std::vector<int>;
constexpr int N = 200000;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = 998244353;
namespace IO {
#ifdef FILEIO
#define IS is
#define OS os
#else
#define IS stdin
#define OS stdout
#endif
FILE *is,*os;
constexpr int IOLIM = 1 << 14;
char ibuf[IOLIM],obuf[IOLIM];
char *iptr,*optr;
inline void InitIO() {
#ifdef FILEIO
is = fopen(".in","r");
os = fopen(".out","w");
#endif
iptr = ibuf + IOLIM;
optr = obuf;
}
inline void EndIO() {
fwrite(obuf,sizeof(char),optr - obuf,OS);
#ifdef FILEIO
fclose(is),fclose(os);
#endif
}
inline char GetC() {
if(__builtin_expect(iptr == ibuf + IOLIM,0)) {
int sz = fread(ibuf,sizeof(char),IOLIM,IS);
if(sz < IOLIM) std::fill(ibuf + sz,ibuf + IOLIM,EOF);
iptr = ibuf;
}
return *iptr++;
}
inline void PutC(char ch) {
if(__builtin_expect(optr == obuf + IOLIM,0)) {
fwrite(obuf,IOLIM,1,OS);
optr = obuf;
}
*optr++ = ch;
}
inline int readI() {
int res = 0;char ch;bool m = 1;
while((ch = GetC()) < '-')
if(ch == EOF) return -1;
if(ch == '-') m = 0,ch = GetC();
while(isdigit(ch)) {
res = (res * 10) + (ch ^ 48);
ch = GetC();
}
return m ? res : -res;
}
inline void writeI(int x) {
if(x < 0) PutC('-'),x = -x;
static int stk[20];int top = 0;
do
stk[top++] = x % 10,x /= 10;
while(x);
while(top) PutC(stk[--top] + 48);
}
}
using IO::InitIO;
using IO::EndIO;
using IO::readI;
using IO::writeI;
using IO::PutC;
namespace Poly {
#define add(x,y) ((((x) += (y)) >= MOD) && ((x) -= MOD))
#define sub(x,y) ((((x) -= (y)) < 0) && ((x) += MOD))
inline i64 qpow(i64 a,i64 b = MOD - 2,i64 m = MOD) {
i64 res = 1;
for(;b;b >>= 1) {
if(b & 1) res = res * a % m;
a = a * a % m;
}
return res;
}
int g[N],iv[N];
inline void InitRoot() {
int t = 17;
g[0] = 1, g[1 << t] = qpow(qpow(3,119),1 << (21 - t));
repb(i,t,1)
g[1 << (i - 1)] = (i64)g[1 << i] * g[1 << i] % MOD;
repl(i,1,(1 << t))
g[i] = (i64)g[i & (i - 1)] * g[i & -i] % MOD;
t = 1 << t,iv[0] = 1;
rep(i,1,t) iv[i] = (i64)iv[i - 1] * i % MOD;
iv[t] = qpow(iv[t]);
repb(i,t,1) {
int t = iv[i - 1];
iv[i - 1] = (i64)iv[i] * i % MOD;
iv[i] = (i64)iv[i] * t % MOD;
}
}
inline void DIF(int *a,const int n) {
for(int l = n >> 1;l;l >>= 1) {
int *p = a;
for(int *G = g;p != a + n;p += (l << 1),++G) {
for(int *x = p;x != p + l;++x) {
int t = (i64)x[l] * *G % MOD;
x[l] = (*x + MOD - t) % MOD,add(*x,t);
}
}
}
}
inline void DIT(int *a,const int n) {
for(int l = 1;l ^ n;l <<= 1) {
int *p = a;
for(int *G = g;p != a + n;p += (l << 1),++G) {
for(int *x = p;x != p + l;++x) {
int t = x[l];
x[l] = (i64)(*x + MOD - t) * *G % MOD,add(*x,t);
}
}
}
repl(i,0,n) a[i] = (i64)a[i] * iv[n] % MOD;
std::reverse(a + 1,a + n);
}
void PolyMul(const int *a,const int *b,int *c,int n,int m) {
static int x[N],y[N];int slen = 1;
while(slen < (n + m - 1)) slen <<= 1;
std::copy_n(a,n,x),std::fill(x + n,x + slen,0);
std::copy_n(b,m,y),std::fill(y + m,y + slen,0);
DIF(x,slen),DIF(y,slen);
repl(i,0,slen) c[i] = (i64)x[i] * y[i] % MOD;
DIT(c,slen),std::fill(c + n + m - 1,c + slen,0);
}
void PolyInv(const int *a,int *b,const int n) {
static int A[N],B[N],tmp[N];int slen = 1;
while(slen < n) slen <<= 1;
std::fill(B,B + slen,0),std::fill(tmp,tmp + slen,0);
tmp[0] = qpow(a[0]);
for(int d = 1,l = 2;d ^ slen;d <<= 1,l <<= 1) {
std::copy_n(a,std::min(l,slen),A),std::copy_n(tmp,d,B);
DIF(A,l),DIF(B,l);
{repl(i,0,l) A[i] = (i64)A[i] * B[i] % MOD;}DIT(A,l);
std::fill(A,A + d,0),DIF(A,l);
{repl(i,0,l) A[i] = (i64)A[i] * B[i] % MOD;}DIT(A,l);
std::copy(A + d,A + l,tmp + d);
repl(i,d,d << 1) tmp[i] = tmp[i] ? MOD - tmp[i] : 0;
}
std::copy_n(tmp,slen,b);
}
inline void PolyDif(const int *a,int *b,const int n) {
repl(i,1,n) b[i - 1] = (i64)a[i] * i % MOD;
b[n - 1] = 0;
}
inline void PolyInte(const int *a,int *b,const int n) {
repb(i,n - 1,1) b[i] = (i64)a[i - 1] * iv[i] % MOD;
b[0] = 0;
}
inline void PolyLn(const int *a,int *b,const int n) {
static int c[N],v[N];std::fill(v,v + (n << 1),0);
PolyDif(a,c,n),PolyInv(a,v,n);
PolyMul(c,v,c,n,n),PolyInte(c,b,n);
}
void PolyExp(const int *a,int *b,const int n) {
if(n == 1) return b[0] = 1,void();
PolyExp(a,b,(n + 1) >> 1);static int c[N];
PolyLn(b,c,n);
int slen = 1;while(slen < (n << 1)) slen <<= 1;
{repl(i,0,n) c[i] = (a[i] + MOD - c[i]) % MOD;}++c[0];
std::fill(c + n,c + slen,0);
PolyMul(b,c,b,n,n),std::fill(b + n,b + slen,0);
}
void PolyDiv(const int *a,const int *b,int *q,int *r,const int n,const int m) {
static int fr[N],gr[N],qr[N],v[N];std::fill(v,v + (n << 1),0);
repl(i,0,n) fr[i] = a[n - i - 1],gr[i] = b[m - i - 1];
PolyInv(gr,v,n - m + 1),PolyMul(fr,v,qr,n - m + 1,n - m + 1);
for(int i = n - m + 1;qr[i];++i) qr[i] = 0;
rep(i,0,n - m) q[i] = qr[n - m - i];
repl(i,n - m + 1,m) q[i] = 0;
PolyMul(b,q,r,m - 1,m - 1);
repl(i,0,m - 1) r[i] = (a[i] - r[i] + MOD) % MOD;
}
#undef add
#undef sub
}
using Poly::InitRoot;
namespace MultiEvaluation {
using namespace Poly;
vec PolyMul(const vec & a,const vec & b) {
int slen = 1,n = a.size(),m = b.size();
static int x[N],y[N];vec c(n + m - 1);
while(slen < (n + m - 1)) slen <<= 1;
std::copy_n(a.begin(),n,x),std::fill(x + n,x + slen,0);
std::copy_n(b.begin(),m,y),std::fill(y + m,y + slen,0);
DIF(x,slen),DIF(y,slen);
repl(i,0,slen) y[i] = (i64)x[i] * y[i] % MOD;
DIT(y,slen),std::copy_n(y,n + m - 1,c.begin());
return c;
}
//Transposition of convolution
vec PolyMulT(const vec & a,const vec & b) {
int slen = 1,n = a.size(),m = b.size();
static int x[N],y[N];vec c(n);
while(slen < (n + m - 1)) slen <<= 1;
std::reverse_copy(a.begin(),a.end(),x),std::fill(x + n,x + slen,0);
std::copy_n(b.begin(),m,y),std::fill(y + m,y + slen,0);
DIF(x,slen),DIF(y,slen);
repl(i,0,slen) y[i] = (i64)x[i] * y[i] % MOD;
DIT(y,slen),std::reverse_copy(y,y + n,c.begin());
return c;
}
vec F[20],GT[256005];
// Calc G in F(x) / G(x)
void MultiEvalInit(const int p,const int l,const int r,const int *x) {
if(l == r) {
GT[p].emplace_back(1);
GT[p].emplace_back((MOD - x[l]) % MOD);
return ;
}
int mid = (l + r) >> 1;
MultiEvalInit(p << 1,l,mid,x);
MultiEvalInit(p << 1 | 1,mid + 1,r,x);
GT[p] = PolyMul(GT[p << 1],GT[p << 1 | 1]);
}
// Calc F & Evaluate
void MultiEvalSolve(const int p,const int l,const int r,const int d,int *y) {
if(l == r) return y[l] = F[d][0],void();
int mid = (l + r) >> 1;
F[d].resize(r - l + 1);
F[d + 1] = PolyMulT(F[d],GT[p << 1 | 1]);
MultiEvalSolve(p << 1,l,mid,d + 1,y);
F[d + 1] = PolyMulT(F[d],GT[p << 1]);
MultiEvalSolve(p << 1 | 1,mid + 1,r,d + 1,y);
}
// Multiple Points Evaluation
void MultiEval(const int *f,const int *x,int *y,const int n,const int m) {
MultiEvalInit(1,0,m - 1,x);
int slen = 1;while(slen < n) slen <<= 1;
GT[1].resize(std::max(n,m));
static int t[N];
std::copy(GT[1].begin(),GT[1].end(),t);
PolyInv(t,t,std::max(n,m));
vec AF(n),AT(std::max(n,m));
std::copy(f,f + n,AF.begin());
std::copy(t,t + std::max(n,m),AT.begin());
F[0] = PolyMulT(AF,AT);
MultiEvalSolve(1,0,m - 1,0,y);
}
}
using MultiEvaluation::MultiEval;
namespace LinearRecurrence {
using namespace Poly;
#define add(x,y) ((((x) += (y)) >= MOD) && ((x) -= MOD))
#define sub(x,y) ((((x) -= (y)) < 0) && ((x) += MOD))
//Linear Recurrence
int LR(const int *a,const int *f,int n,int k) {
static int L[N],F[N],A[N],Q[N],R[N];
repb(i,k - 1,0) L[i] = (MOD - f[k - i - 1]) % MOD;
L[k] = 1,F[0] = 1,A[1] = 1;
for(;n;n >>= 1) {
if(n & 1) {
PolyMul(F,A,F,k,k);
PolyDiv(F,L,Q,R,k << 1,k + 1);
std::copy_n(R,k,F);
}
PolyMul(A,A,A,k,k);
PolyDiv(A,L,Q,R,k << 1,k + 1);
std::copy_n(R,k,A);
}
int ans = 0;
repl(i,0,k) add(ans,(i64)F[i] * a[i] % MOD);
return ans;
}
//Berlekamp Massey
int BM(const int *f,int *p,int n) {
static int lstf[N],curf[N],tmpf[N];
int k = 0,w = 0,lstk = 0;
i64 d = 0;
for(int i = 1;i <= n;++i) {
i64 tmp = 0;
for(int j = 0;j < k;++j)
add(tmp,(i64)f[i - j - 1] * curf[j] % MOD);
if((((i64)f[i] - tmp + MOD) % MOD) == 0)
continue;
if(!w) {
w = i;
d = ((i64)f[i] - tmp + MOD) % MOD;
for(int j = 1;j <= i;++j)
curf[k++] = 0;
continue;
}
for(int j = 0;j < k;++j)
tmpf[j] = curf[j];
int tmpk = k;
i64 mul = (((i64)f[i] - tmp + MOD) % MOD * qpow(d)) % MOD;
if(k < lstk + i - w)
k = lstk + i - w;
add(curf[i - w - 1],mul);
for(int j = 0;j < lstk;++j)
sub(curf[i - w + j],(i64)lstf[j] * mul % MOD);
if(tmpk - i < k - w) {
for(int j = 0;j < tmpk;++j)
lstf[j] = tmpf[j];
lstk = tmpk,w = i,d = ((i64)f[i] - tmp + MOD) % MOD;
}
}
for(int i = 0;i < k;++i)
p[i] = curf[i];
return k;
}
int CalcSeq(const int n,const int m,const int *f) {
static int a[N];
int k = BM(f,a,n);
return LR(f + 1,a,m,k);
}
#undef add
#undef sub
}
using LinearRecurrence::BM;
using LinearRecurrence::LR;
using LinearRecurrence::CalcSeq;
int main() {
InitIO();
InitRoot();
EndIO();
return 0;
}
#undef rep
#undef repb
#undef repl
#undef repr
#undef repg
#undef mems
#undef space
#undef enter
#undef FILEIO
#undef DEBUG