版本比较
poly publish
正文
增加 389 行删除 1 行
1123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389d```cppnamespace quad_residue{ typedef long long LL; mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); struct P { LL a,b; friend P mul (const P &x,const P &y,const LL &z,const LL &mod) { return (P){(x.a * y.a % mod + x.b * y.b % mod * z % mod) % mod,(x.a * y.b % mod + x.b * y.a % mod) % mod}; } friend P qmi(P a,LL k,LL x,LL mod) { P res = {1,0}; while(k) { if(k & 1) res = mul(res,a,x,mod); k >>= 1; a = mul(a,a,x,mod); } return res; } }; LL qmi(LL a,LL k,LL mod) { LL res = 1; while(k) { if(k & 1) res = res * a % mod; k >>= 1; a = a * a % mod; } return res; } bool check(LL a,LL mod) { return qmi(a,(mod - 1) / 2,mod) == 1; } LL get(LL a,LL mod) { if(!a) return 0; if(!check(a,mod)) return -1; LL r = rd() % mod; while(check((r * r % mod - a + mod) % mod,mod)) r = rd() % mod; return qmi((P){r,1},(mod + 1) / 2,(r * r % mod - a + mod) % mod,mod).a; }}namespace polynomial{ const int N = (1 << 19) + 10; typedef long long LL; const int mod = 998244353,half = 499122177; const double pi = acos(-1); const int G[] = {332748118,3}; LL qmi(LL x,LL k) { LL res = 1; while(k) { if(k & 1) res = res * x % mod; k >>= 1; x = x * x % mod; } return res; } struct ntt_base { int n,m,c; LL a[N],b[N],r[N],lim = 1,l; ntt_base() { n = m = c = l = 0; memset(a,0,sizeof a); memset(b,0,sizeof b); memset(r,0,sizeof r); } void clear() { n = m = c = l = 0; lim = 1; memset(a,0,sizeof a); memset(b,0,sizeof b); memset(r,0,sizeof r); } void NTT(LL A[],int type) { for(int i = 0; i < lim; i ++) if(i < r[i]) swap(A[i],A[r[i]]); for(int mid = 1; mid < lim; mid <<= 1) { LL w_n = qmi(G[type == 1],(mod - 1) / (mid << 1)); for(int j = 0,R = mid << 1; j < lim; j += R) { LL w = 1; for(int k = 0; k < mid; k ++,w = w * w_n % mod) { LL x = A[j + k],y = w * A[j + mid + k] % mod; A[j + k] = (x + y) % mod; A[j + mid + k] = (x - y + mod) % mod; } } } } }; struct fwt_base { int n; LL a[N],b[N],lim = 1,l; fwt_base() { n = l = 0; lim = 1; memset(a,0,sizeof a); memset(b,0,sizeof b); } void clear() { n = l = 0; lim = 1; memset(a,0,sizeof a); memset(b,0,sizeof b); } void FWT_or(LL A[],int type) { for(int mid = 1; mid < lim; mid <<= 1) { for(int j = 0,R = mid << 1; j < lim; j += R) { for(int k = 0; k < mid; k ++) { A[j + mid + k] = ((A[j + mid + k] + A[j + k] * type) % mod + mod) % mod; } } } } void FWT_and(LL A[],int type) { for(int mid = 1; mid < lim; mid <<= 1) { for(int j = 0,R = mid << 1; j < lim; j += R) { for(int k = 0; k < mid; k ++) { A[j + k] = ((A[j + k] + A[j + mid + k] * type) % mod + mod) % mod; } } } } void FWT_xor(LL A[],int type) { for(int mid = 1; mid < lim; mid <<= 1) { for(int j = 0,R = mid << 1; j < lim; j += R) { for(int k = 0; k < mid; k ++) { LL x = A[j + k],y = A[j + mid + k]; A[j + k] = ((x + y) * type % mod + mod) % mod; A[j + mid + k] = ((x - y) * type % mod + mod) % mod; } } } } }; struct poly { int n; LL a[N]; poly() { n = 0; memset(a,0,sizeof a); } void clear() { n = 0; memset(a,0,sizeof a); } void check() { while(n && a[n] == 0) n --; } friend ostream& operator << (ostream &out,const poly &x) { for(int i = 0; i <= x.n; i ++) out<<x.a[i]<<' '; return out; } friend poly operator + (const poly &a,const poly &b) { static poly tmp; tmp.clear(); tmp.n = max(a.n,b.n); for(int i = 0; i <= max(a.n,b.n); i ++) tmp.a[i] = (a.a[i] + b.a[i]) % mod; return tmp; } friend poly operator - (const poly &a,const poly &b) { static poly tmp; tmp.clear(); tmp.n = max(a.n,b.n); for(int i = 0; i <= max(a.n,b.n); i ++) tmp.a[i] = ((a.a[i] - b.a[i]) % mod + mod) % mod; return tmp; } friend void mul(const poly &a,const poly &b,poly &c) { static ntt_base ntt; ntt.clear(); ntt.n = a.n,ntt.m = b.n; for(int i = 0; i <= ntt.n; i ++) ntt.a[i] = a.a[i]; for(int i = 0; i <= ntt.m; i ++) ntt.b[i] = b.a[i]; while(ntt.lim <= ntt.n + ntt.m) ntt.lim <<= 1,ntt.l ++; for(int i = 0; i < ntt.lim; i ++) ntt.r[i] = (ntt.r[i >> 1] >> 1) | ((i & 1) << (ntt.l - 1)); ntt.NTT(ntt.a,1),ntt.NTT(ntt.b,1); for(int i = 0; i < ntt.lim; i ++) ntt.a[i] = ntt.a[i] * ntt.b[i] % mod; ntt.NTT(ntt.a,-1); LL inv = qmi(ntt.lim,mod - 2); c.n = ntt.n + ntt.m; for(int i = 0; i <= c.n; i ++) c.a[i] = ntt.a[i] * inv % mod; } friend void fwt_or(const poly &a,const poly &b,poly &c) { static fwt_base fwt; fwt.clear(); fwt.n = a.n + 1; fwt.lim = 1; while(fwt.lim < fwt.n) fwt.lim <<= 1; for(int i = 0; i < fwt.n; i ++) fwt.a[i] = a.a[i]; for(int i = 0; i < fwt.n; i ++) fwt.b[i] = b.a[i]; for(int i = fwt.n; i < fwt.lim; i ++) fwt.a[i] = fwt.b[i] = 0; fwt.FWT_or(fwt.a,1),fwt.FWT_or(fwt.b,1); for(int i = 0; i < fwt.lim; i ++) fwt.a[i] = fwt.a[i] * fwt.b[i] % mod; fwt.FWT_or(fwt.a,-1); c.n = fwt.n - 1; for(int i = 0; i <= c.n; i ++) c.a[i] = fwt.a[i] % mod; } friend void fwt_and(const poly &a,const poly &b,poly &c) { static fwt_base fwt; fwt.clear(); fwt.n = a.n + 1; fwt.lim = 1; while(fwt.lim < fwt.n) fwt.lim <<= 1; for(int i = 0; i < fwt.n; i ++) fwt.a[i] = a.a[i]; for(int i = 0; i < fwt.n; i ++) fwt.b[i] = b.a[i]; for(int i = fwt.n; i < fwt.lim; i ++) fwt.a[i] = fwt.b[i] = 0; fwt.FWT_and(fwt.a,1),fwt.FWT_and(fwt.b,1); for(int i = 0; i < fwt.lim; i ++) fwt.a[i] = fwt.a[i] * fwt.b[i] % mod; fwt.FWT_and(fwt.a,-1); c.n = fwt.n - 1; for(int i = 0; i <= c.n; i ++) c.a[i] = fwt.a[i] % mod; } friend void fwt_xor(const poly &a,const poly &b,poly &c) { static fwt_base fwt; fwt.clear(); fwt.n = a.n + 1; fwt.lim = 1; while(fwt.lim < fwt.n) fwt.lim <<= 1; for(int i = 0; i < fwt.n; i ++) fwt.a[i] = a.a[i]; for(int i = 0; i < fwt.n; i ++) fwt.b[i] = b.a[i]; for(int i = fwt.n; i < fwt.lim; i ++) fwt.a[i] = fwt.b[i] = 0; fwt.FWT_xor(fwt.a,1),fwt.FWT_xor(fwt.b,1); for(int i = 0; i < fwt.lim; i ++) fwt.a[i] = fwt.a[i] * fwt.b[i] % mod; fwt.FWT_xor(fwt.a,half); c.n = fwt.n - 1; for(int i = 0; i <= c.n; i ++) c.a[i] = fwt.a[i] % mod; } friend void inv(const poly &a,const int &n,poly &b) { static ntt_base ntt; ntt.clear(); if(n == 1) { b.a[0] = qmi(a.a[0],mod - 2); b.n = 0; return; } inv(a,n + 1 >> 1,b); ntt.n = n,ntt.m = n; while(ntt.lim <= ntt.n + ntt.m) ntt.lim <<= 1,ntt.l ++; for(int i = 0; i < n; i ++) ntt.a[i] = a.a[i]; for(int i = n; i < ntt.lim; i ++) ntt.a[i] = 0; for(int i = 0; i < (n + 1 >> 1); i ++) ntt.b[i] = b.a[i]; for(int i = (n + 1 >> 1); i < ntt.lim; i ++) ntt.b[i] = 0; for(int i = 0; i < ntt.lim; i ++) ntt.r[i] = (ntt.r[i >> 1] >> 1) | ((i & 1) << (ntt.l - 1)); ntt.NTT(ntt.a,1),ntt.NTT(ntt.b,1); for(int i = 0; i < ntt.lim; i ++) ntt.a[i] = (2 - ntt.a[i] * ntt.b[i] % mod + mod) % mod * ntt.b[i] % mod; ntt.NTT(ntt.a,-1); LL inv = qmi(ntt.lim,mod - 2); b.n = n - 1; for(int i = 0; i < n; i ++) b.a[i] = ntt.a[i] * inv % mod; } friend void div(poly a,poly b,poly &d,poly &e) { static poly tmp; tmp.clear(); int l = a.n - b.n + 1; tmp.n = l - 1; reverse(a.a,a.a + a.n + 1); reverse(b.a,b.a + b.n + 1); inv(b,l,tmp); mul(tmp,a,d); d.n = l - 1; reverse(d.a,d.a + d.n + 1); reverse(a.a,a.a + a.n + 1); reverse(b.a,b.a + b.n + 1); mul(b,d,tmp); e = a - tmp; e.n = b.n - 1; } friend void sqrt(const poly &a,const int &n,poly &b) { static poly tmp,tmp2,tmp3; if(n == 1) { b.a[0] = 1; LL res = quad_residue::get(a.a[0],mod); b.a[0] = min(res,mod - res); b.n = 0; return; } sqrt(a,n + 1 >> 1,b); for(int i = (n + 1 >> 1); i < n; i ++) b.a[i] = 0; b.n = n - 1; tmp.n = tmp2.n = n; for(int i = 0; i < n; i ++) tmp.a[i] = b.a[i] * 2 % mod; inv(tmp,n,tmp2); b.n = n; mul(b,b,tmp3); for(int i = 0; i < n; i ++) b.a[i] = tmp3.a[i]; b.n = n; for(int i = 0; i < n; i ++) b.a[i] = (b.a[i] + a.a[i]) % mod; mul(b,tmp2,tmp3); for(int i = 0; i < n; i ++) b.a[i] = tmp3.a[i]; b.n = n - 1; } friend void diff(const poly &a,poly &b) { b.n = a.n - 1; for(int i = 0; i <= b.n; i ++) b.a[i] = (i + 1) * a.a[i + 1] % mod; } friend void inte(const poly &a,poly &b) { b.n = a.n + 1; b.a[0] = 0; for(int i = 1; i <= b.n; i ++) b.a[i] = a.a[i - 1] * qmi(i,mod - 2) % mod; } friend void ln(const poly &a,const int &n,poly &b) { static poly tmp,tmp2; tmp.clear(),tmp2.clear(); diff(a,tmp); inv(a,n,b); tmp.n = n - 1; mul(b,tmp,tmp2); tmp2.n = n - 2; inte(tmp2,b); } friend void exp(const poly &a,const int &n,poly &b) { static poly tmp,tmp2; if(n == 1) { b.a[0] = 1; b.n = 0; return; } exp(a,n + 1 >> 1,b); tmp.n = tmp2.n = n; ln(b,n,tmp); tmp.a[0] --; tmp2 = a - tmp; tmp.n = tmp2.n = n; mul(tmp2,b,tmp); for(int i = 0; i < n; i ++) b.a[i] = tmp.a[i]; b.n = n - 1; } friend void qmi(const poly &a,const LL &k,poly &b) { static poly tmp; tmp.clear(); ln(a,a.n + 1,tmp); for(int i = 0; i <= tmp.n; i ++) tmp.a[i] = tmp.a[i] * k % mod; b.n = a.n; exp(tmp,a.n + 1,b); } };}