首页 > 代码库 > bzoj3625 [Codeforces Round #250]小朋友和二叉树

bzoj3625 [Codeforces Round #250]小朋友和二叉树

传送门

终于学会多项式开根了哈哈……

反正题解都烂大街了,我就不写了,直接贴代码算了……(犯懒ing)

技术分享
 1 /**************************************************************
 2     Problem: 3625
 3     User: _Angel_
 4     Language: C++
 5     Result: Accepted
 6     Time:29048 ms
 7     Memory:5944 kb
 8 ****************************************************************/
 9 #include<cstdio>
10 #include<cstring>
11 #include<algorithm>
12 using namespace std;
13 const int maxn=262200,p=998244353,g=3,inv_2=499122177;
14 void NTT(int*,int,int);
15 void getsqrt(int*,int*,int);
16 void getinv(int*,int*,int);
17 int qpow(int,int,int);
18 int n,N=1,m,x,A[maxn],B[maxn];
19 int main(){
20     scanf("%d%d",&m,&n);
21     while(N<=n)N<<=1;
22     while(m--){
23         scanf("%d",&x);
24         A[x]=(A[x]-4+p)%p;
25     }
26     A[0]=(A[0]+1)%p;
27     getsqrt(A,B,N);
28     B[0]=(B[0]+1)%p;
29     getinv(B,A,N);
30     for(int i=1;i<=n;i++)printf("%d\n",(int)(A[i]*2ll%p));
31     return 0;
32 }
33 void NTT(int *A,int n,int tp){
34     for(int i=1,j=0,k;i<n-1;i++){
35         k=n;
36         do j^=(k>>=1);while(j<k);
37         if(i<j)swap(A[i],A[j]);
38     }
39     for(int k=2;k<=n;k<<=1){
40         int wn=qpow(g,(tp>0?(p-1)/k:(p-1)/k*(long long)(p-2)%(p-1)),p);
41         for(int i=0;i<n;i+=k){
42             int w=1;
43             for(int j=0;j<(k>>1);j++,w=(long long)w*wn%p){
44                 int a=A[i+j],b=(long long)w*A[i+j+(k>>1)]%p;
45                 A[i+j]=(a+b)%p;
46                 A[i+j+(k>>1)]=(a-b+p)%p;
47             }
48         }
49     }
50     if(tp<0){
51         int inv=qpow(n,p-2,p);
52         for(int i=0;i<n;i++)A[i]=(long long)A[i]*inv%p;
53     }
54 }
55 void getsqrt(int *A,int *C,int n){
56     static int B[maxn],D[maxn];
57     fill(C,C+(n<<1),0);
58     C[0]=1;
59     for(int k=2;k<=n;k<<=1){
60         copy(A,A+k,B);
61         fill(B+k,B+(k<<1),0);
62         getinv(C,D,k);
63         NTT(B,k<<1,1);
64         NTT(D,k<<1,1);
65         for(int i=0;i<(k<<1);i++)B[i]=(long long)B[i]*D[i]%p;
66         NTT(B,k<<1,-1);
67         for(int i=0;i<k;i++)C[i]=(C[i]+B[i])%p*(long long)inv_2%p;
68     }
69 }
70 void getinv(int *A,int *C,int n){
71     static int B[maxn];
72     fill(C,C+(n<<1),0);
73     C[0]=qpow(A[0],p-2,p);
74     for(int k=2;k<=n;k<<=1){
75         copy(A,A+k,B);
76         fill(B+k,B+(k<<1),0);
77         NTT(B,k<<1,1);
78         NTT(C,k<<1,1);
79         for(int i=0;i<(k<<1);i++)C[i]=C[i]*((2-(long long)B[i]*C[i]%p+p)%p)%p;
80         NTT(C,k<<1,-1);
81         fill(C+k,C+(k<<1),0);
82     }
83 }
84 int qpow(int a,int b,int p){
85     int ans=1;
86     for(;b;b>>=1,a=(long long)a*a%p)if(b&1)ans=(long long)ans*a%p;
87     return ans;
88 }
View Code

 

bzoj3625 [Codeforces Round #250]小朋友和二叉树