Poly

使用注意事项:

  1. 需要搭配 ModNum 里面的 MInt 使用。
  2. 要先运行Prework,参数是多项式最高度数,有时候答案不对可能是初始化小了,开大点试试。
  3. NTT 的模数不同需要更改 Prework 的参数,若模数为 \(r \times 2^k + 1\),那么 \(GT\) 改为 \(k - 2\)\(GR\) 改为 \(g^r\),其中 \(g\) 为模数的原根。
  4. 赋值用 .pin,输出用 .pout
  5. 多项式封装了一个 poly 类,里头是自取模的 ModInt 类,模数不同需要修改 Mod

各函数意义:

  1. 乘法直接用 *;除法如果同时求商和余数用 Div,返回一个 poly 二元组,否则直接用 / 或者 % 即可。
  2. Inv 为求逆,Dvt 为求导,Igt 为积分。
  3. Ln 为求对数,Exp 为求指数。
  4. Sqr 为开方,传参 \(tp = 1\) 返回 \(\sqrt{a}\)\(tp = -1\) 返回 \(\dfrac{1}{\sqrt{a}}\)
  5. Pow 为快速幂,第一个非 \(0\) 项为常数,传幂次直接传数值,否则传进去一个字符串。
  6. 三角函数和反三角函数同函数名。
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
namespace Poly
{
    static const int MAX=4000010;
    static const double PI=acos(-1);
    static const ModInt<Mod> gn=332748118,gni=3,ZERO=0,ONE=1;

    namespace Quadraticresidue
    {
        ModInt<Mod> I;
        struct Complex
        {
            ModInt<Mod> x,y;
            Complex(ModInt<Mod> X=0,ModInt<Mod> Y=0):x(X),y(Y) {}
            inline friend bool operator == (Complex a,Complex b) {return a.x==b.x&&a.y==b.y;}
            inline friend Complex operator * (Complex a,Complex b) {return Complex(a.x*b.x+a.y*b.y*I,a.x*b.y+a.y*b.x);}
        };
        inline Complex qpow(Complex a,int b) {Complex res(1,0); while(b) {if(b&1) res=res*a; a=a*a; b>>=1;} return res;}
        inline ModInt<Mod> random(int l,int r)
        {
            static mt19937_64 sd(20070707);
            static uniform_int_distribution<int> range(l,r);
            return ModInt<Mod>(range(sd));
        }
        inline pair<ModInt<Mod>,ModInt<Mod>> Cipolla(ModInt<Mod> n)
        {
            if(!n) return make_pair(0,0);
            ModInt<Mod> x,x1,x2;
            do x=random(1,Mod-1); while(((x*x+Mod-n)^((Mod-1)>>1))==ONE);
            I=x*x+Mod-n,x1=qpow(Complex(x,1),(Mod+1)>>1).x,x2=Mod-x1;
            return make_pair(min(x1,x2),max(x1,x2));
        }
    }
    using namespace Quadraticresidue;

    struct poly
    {
        vector<ModInt<Mod>> a;
        inline ModInt<Mod> &operator [] (int i) {return a[i];}
        inline poly operator = (const poly &T) {this->a=T.a; return *this;}
        inline int size()         {return a.size();}
        inline void resize(int N) {return a.resize(N),void();}
        inline void reverse()     {return std::reverse(a.begin(),a.end());}
        inline void pin(int x)    {return a.emplace_back(ModInt<Mod>(x)),void();}
        inline void pout()        {for(auto it:a) write(it.x,' ');}
    };

    inline int Sup(int N) {int K=1; while(K<N) K<<=1; return K;}

    static const int GT=21,GR=31;
    ModInt<Mod> omega[MAX]; poly zero,one;
    inline void Prework(int N)
    {
        zero.a.emplace_back(ZERO),one.a.emplace_back(ONE);
        int K=1; while((1<<K)<N) ++K; K=min(K-1,21ll);
        omega[0]=1,omega[1<<K]=(ModInt<Mod>(GR))^(1<<(GT-K));
        for(int i=K;i>=1;--i) omega[1<<(i-1)]=omega[1<<i]*omega[1<<i];
        for(int i=1;i<(1<<K);++i) omega[i]=omega[i&(i-1)]*omega[i&(-i)];
    }
    inline void NTT(poly &F,int typ)
    {
        ModInt<Mod> U,V;
        int N=F.size();
        if(typ==1)
        {
            for(int mid=N>>1;mid>=1;mid>>=1)
                for(int i=0,k=0;i<N;i+=mid<<1,++k)
                    for(int j=0;j<mid;++j)
                        U=F[i+j],V=F[i+j+mid]*omega[k],
                        F[i+j]=U+V,F[i+j+mid]=U-V;
        }
        if(typ==-1)
        {
            for(int mid=1;mid<N;mid<<=1)
                for(int i=0,k=0;i<N;i+=mid<<1,++k)
                    for(int j=0;j<mid;++j)
                        U=F[i+j],V=F[i+j+mid],
                        F[i+j]=U+V,F[i+j+mid]=(U-V)*omega[k];
            ModInt<Mod> Ninv=ONE/N;
            for(int i=0;i<N;++i) F[i]*=Ninv;
            reverse(F.a.begin()+1,F.a.end());
        }
    }
    inline poly operator * (poly a,ModInt<Mod> b) {for(int i=0;i<a.size();++i) a[i]*=b; return a;}
    inline poly operator / (poly a,ModInt<Mod> b) {for(int i=0;i<a.size();++i) a[i]/=b; return a;}
    inline poly operator - (poly a) {for(int i=0;i<a.size();++i) a[i]=-a[i]; return a;}
    inline poly operator + (poly a,poly b)
    {
        int N=max(a.size(),b.size());
        a.resize(N),b.resize(N);
        for(int i=0;i<N;++i) a[i]+=b[i];
        return a;
    }
    inline poly operator - (poly a,poly b)
    {
        int N=max(a.size(),b.size());
        a.resize(N),b.resize(N);
        for(int i=0;i<N;++i) a[i]-=b[i];
        return a;
    }
    inline poly operator * (poly a,poly b)
    {
        int K=a.size()+b.size()-1,N=Sup(K);
        a.resize(N),b.resize(N);
        NTT(a,1),NTT(b,1);
        for(int i=0;i<N;++i) a[i]*=b[i];
        NTT(a,-1);
        a.resize(K);
        return a;
    }
    inline poly operator >> (poly a,int d)
    {
        int N=a.size(); poly b; 
        b.resize(N+d);
        for(int i=0;i<N;++i) b[i+d]=a[i];
        return b; 
    }
    inline poly operator << (poly a,int d)
    {
        int N=a.size(); if(N<=d) return zero;
        poly b; b.resize(N-d);
        for(int i=0;i<N-d;++i) b[i]=a[i+d];
        return b;
    }
    inline poly Dvt(poly a)
    {
        int N=a.size()-1;
        for(int i=0;i<N;++i) a[i]=a[i+1]*(i+1);
        return a.resize(N),a;
    }
    inline poly Itg(poly a)
    {
        int N=a.size()+1; a.resize(N);
        for(int i=N;i>=1;--i) a[i]=a[i-1]/i;
        return a[0]=0,a;
    }
    inline poly Inv(poly a)
    {
        int K=a.size(),N=Sup(K);
        poly b,c,d; a.resize(N),b.resize(N);
        b[0]=ONE/a[0];
        for(int len=2;len<=N;len<<=1)
        {
            c.resize(len),d.resize(len);
            for(int i=0;i<len;++i) c[i]=a[i];
            for(int i=0;i<(len>>1);++i) d[i]=b[i];
            NTT(c,1),NTT(d,1);
            for(int i=0;i<len;++i) c[i]=c[i]*d[i];
            NTT(c,-1); for(int i=1;i<(len>>1);++i) c[i]=0; c[0]=Mod-1;
            NTT(c,1);  for(int i=0;i<len;++i) d[i]=d[i]*c[i];
            NTT(d,-1); for(int i=len>>1;i<len;++i) b[i]=-d[i];
        }
        b.resize(K);
        return b;
    }
    inline poly operator / (poly a,poly b)
    {
        int N=a.size(),M=b.size();
        poly c=a,d=b;
        c.reverse(),d.reverse();
        d.resize(N-M+1),d=Inv(d);
        c=c*d;c.resize(N-M+1),c.reverse();
        return c;
    }
    inline poly operator % (poly a,poly b)
    {
        int M=b.size();
        poly q=a/b; b=b*q;
        poly r=a-b; r.resize(M-1);
        return r;
    }
    inline pair<poly,poly> Div(poly a,poly b)
    {
        int M=b.size();
        poly q=a/b; b=b*q;
        poly r=a-b; r.resize(M-1);
        return make_pair(q,r);
    }

    namespace DivideConquer
    {
        poly F[20][8],G[20][8];
        inline void brute(poly &a,poly &b,int l,int r,void work(poly &a,poly &b,int i))
        {
            work(a,b,l);
            for(int i=l+1;i<=r;++i) 
            {
                for(int j=l;j<i;++j) a[i]+=a[j]*b[i-j];
                work(a,b,i);
            }
        }
        inline void Divide(poly &a,poly &b,int l,int r,int dep,void work(poly &a,poly &b,int i))
        {
            if(r-l+1<=32) return brute(a,b,l,r,work);
            if(l>=r) return;
            int coef=1<<((dep-1)*3);
            poly tmp; tmp.resize(coef<<1);
            for(int i=0;;++i)
            {
                int L=l+i*coef,R=min(r,L+coef-1);
                if(i)
                {
                    for(int j=0;j<(coef<<1);++j) tmp[j]=0;
                    for(int j=0;j<i;++j) for(int k=0;k<(coef<<1);++k) tmp[k]+=F[dep][j][k]*G[dep][i-j][k];
                    NTT(tmp,-1);
                    for(int j=L;j<=R;++j) a[j]+=tmp[j-L+coef];
                }
                Divide(a,b,L,R,dep-1,work);
                if(R==r) return;
                for(int j=0;j<(coef<<1);++j) F[dep][i][j]=0;
                for(int j=L;j<=R;++j) F[dep][i][j-L]=a[j];
                NTT(F[dep][i],1);
            }
        }
        inline void solve(poly &a,poly &b,int N,void work(poly &a,poly &b,int i))
        {
            if(N<=128) return brute(a,b,0,N-1,work);
            int len=1,dep=0;
            while(len<N) len<<=3,++dep;
            len>>=3;
            for(int i=1;i<=dep;++i)
            {
                int coef=1<<((i-1)*3),mix=min((N-1)/coef,7ll);
                for(int j=1;j<=mix;++j)
                {
                    int L=(j-1)*coef+1,R=min(N-1,(j+1)*coef-1);
                    F[i][j-1].resize(coef<<1),G[i][j].resize(coef<<1);
                    for(int k=0;k<(coef<<1);++k) G[i][j][k]=0;
                    for(int k=L;k<=R;++k) G[i][j][k-L+1]=b[k];
                    NTT(G[i][j],1);
                }
            }
            Divide(a,b,0,N-1,dep,work);
        }
    }
    using namespace DivideConquer;

    inline void workln(poly &a,poly &b,int i)
    {
        if(!i) a[i]=0;
        else a[i]=i*b[i]-a[i];
    }
    inline poly Ln(poly a)
    {
        int N=a.size();
        poly b; b.resize(N);
        solve(b,a,N,workln);
        b[0]=0; for(int i=1;i<N;++i) b[i]*=ONE/i;
        return b;
    }
    inline void workexp(poly &a,poly &b,int i)
    {
        if(!i) a[i]=1;
        else a[i]*=ONE/i;
    }
    inline poly Exp(poly a)
    {
        int N=a.size();
        for(int i=0;i<N;++i) a[i]=a[i]*i;
        poly b; b.resize(N);
        solve(b,a,N,workexp);
        return b;
    }
    inline void PrePow(char *s,int &k1,int &k2,int &k3)
    {
        int len=strlen(s+1);
        for(int i=1;i<=len;++i) k1=((k1*10)+(s[i]^48))%Mod,k2=((k2*10)+(s[i]^48))%(Mod-1);
        for(int i=1;i<=min(7ll,len);++i) k3=(k3*10)+(s[i]^48);
    }
    inline poly Pow(poly a,ModInt<Mod> b)
    {
        int N=a.size();
        a=Exp(Ln(a)*b),a.resize(N);
        return a;
    }
    inline poly Pow(poly a,char *s)
    {
        int N=a.size(),lst=N;
        ModInt<Mod> k1,k2,k3;
        PrePow(s,k1.x,k2.x,k3.x);
        poly b; b.resize(N);
        if(a[0]==0&&k3>=N) return b;
        for(int i=0;i<N;++i) if(a[i]!=0) {lst=i; break;}
        if(lst*k1>=N) return b;
        ModInt<Mod> inv=ONE/a[lst],p=a[lst]^(k2.x);
        for(int i=0;i<N-lst;++i) a[i]=a[i+lst]*inv;
        for(int i=N-lst;i<N;++i) a[i]=0;
        a=Ln(a); for(int i=0;i<N;++i) a[i]*=k1;
        a=Exp(a),lst=lst*k1.x; for(int i=N-1;i>=lst;--i) b[i]=a[i-lst]*p;
        return b;
    }
    inline poly Sqr(poly a,int typ)
    {
        int K=a.size(),N=Sup(K);
        poly f,g,h,tmp,res,ans; 
        f.resize(N),g.resize(N),h.resize(N),res.resize(1);
        f[0]=g[0]=Cipolla(a[0]).first,h[0]=ONE/g[0];
        for(int len=2;len<=N;len<<=1)
        {
            tmp.resize(len),ans.resize(len);
            for(int i=0;i<(len>>1);++i) tmp[i]=h[i],res[i]=g[i]*g[i];
            NTT(res,-1); for(int i=0;i<(len>>1);++i) ans[i+(len>>1)]=res[i]-a[i]-a[i+(len>>1)],ans[i]=0;
            NTT(ans,1),NTT(tmp,1); for(int i=0;i<len;++i) ans[i]*=tmp[i]*(ONE/2);
            NTT(ans,-1); for(int i=len>>1;i<len;++i) f[i]=-ans[i];
            if(len!=N||typ==-1) 
            {
                res.resize(len); for(int i=0;i<len;++i) res[i]=f[i];
                NTT(res,1);  for(int i=0;i<len;++i) g[i]=res[i],res[i]*=tmp[i];
                NTT(res,-1); res[0]=Mod-1; for(int i=1;i<(len>>1);++i) res[i]=0;
                NTT(res,1);  for(int i=0;i<len;++i) res[i]*=tmp[i];
                NTT(res,-1); for(int i=len>>1;i<len;++i) h[i]=-res[i];
            }
        }
        f.resize(K),h.resize(K);
        if(typ==1) return f; else return h;
    }
    inline poly sin(poly a)
    {
        ModInt<Mod> E=((ModInt<Mod>(3))^((Mod-1)>>2));
        return (Exp(a*E)-Exp(a*(-E)))*((E*2)^(Mod-2));
    }
    inline poly cos(poly a)
    {
        ModInt<Mod> E=((ModInt<Mod>(3))^((Mod-1)>>2));
        return (Exp(a*E)+Exp(a*(-E)))*((Mod+1)>>1);
    }
    inline poly tan(poly a)
    {
        int N=a.size();
        a=sin(a)*Inv(cos(a)),a.resize(N);
        return a;
    }
    inline poly arcsin(poly a)
    {
        int N=a.size();
        poly b=a*a; b.resize(N),b=-b,b[0]+=1;
        a=Itg(Dvt(a)*Sqr(b,-1)); a.resize(N);
        return a;
    }
    inline poly arctan(poly a)
    {
        int N=a.size();
        poly b=a*a; b.resize(N),b[0]+=1;
        a=Itg(Dvt(a)*Inv(b)); a.resize(N);
        return a;
    }
}