CF755G PolandBall and Many Other Balls

Posted Scx117

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF755G PolandBall and Many Other Balls相关的知识,希望对你有一定的参考价值。

题意:

标程:

技术分享图片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int mod=998244353;
 5 const int rt=31;
 6 const int N=300005;
 7 int l,_n,pos[N],n,k,a[N],b[N],c[N],x1[N],x2[N],x3[N],x4[N],w[N],wn,inv_n;
 8 int ksm(int x,int y)
 9 {
10     int res=1;
11     while (y) {if (y&1) res=(ll)res*x%mod; x=(ll)x*x%mod; y>>=1;} 
12     return res; 
13 }
14 void init(int n)
15 {
16     l=0;
17     while ((1<<l)<=n) l++;//注意位长的处理,判断条件是(1<<l)<=n 
18     _n=1<<l;wn=ksm(rt,1<<23-l);
19     w[0]=1;inv_n=ksm(_n,mod-2);
20     for (int i=1;i<_n;i++)
21       w[i]=(ll)w[i-1]*wn%mod,pos[i]=(i&1)?pos[i-1]|(1<<(l-1)):pos[i>>1]>>1;
22 }
23 void fft(int *a,int op)
24 {
25     for (int i=0;i<_n;i++) if (i<pos[i]) swap(a[i],a[pos[i]]);
26     int len=1,id=_n;
27     for (int i=0;i<l;i++)
28     {
29       int wn=w[id>>=1];
30       for (int j=0;j<_n;j+=len*2)
31         for (int k=j,w=1;k<j+len;k++)
32         {
33              int l=a[k],r=(ll)a[k+len]*w%mod;
34              a[k]=((ll)l+r)%mod;a[k+len]=((ll)l-r+mod)%mod;
35              w=(ll)w*wn%mod;
36          }
37       len<<=1;
38     }
39     if (op==-1) {
40         reverse(a+1,a+_n);
41         for (int i=0;i<_n;i++) a[i]=(ll)a[i]*inv_n%mod; 
42     }
43 }
44 void merge(int *a,int *b,int *c)
45 {
46     c[0]=1;
47     for (int i=1;i<=k;i++) c[i]=((ll)((ll)b[i]+b[i-1])%mod+a[i-1])%mod; 
48     for (int i=k+1;i<_n;i++) c[i]=0;
49 } 
50 void solve(int n)
51 {
52     if (n==0)
53     {
54         a[0]=1;for (int i=1;i<_n;i++) a[i]=0;
55         b[0]=b[1]=1;for (int i=2;i<_n;i++) b[i]=0;
56         return;
57     }
58     if (n==1)
59     {
60         a[0]=a[1]=1;for (int i=2;i<_n;i++) a[i]=0;
61         b[0]=b[2]=1;b[1]=3;for (int i=3;i<_n;i++) b[i]=0;
62         return;
63     }
64     solve(n/2-1);
65     merge(a,b,c);
66    for (int i=k+1;i<_n;i++) a[i]=b[i]=0;
67    fft(a,1);fft(b,1);fft(c,1);
68    for (int i=0;i<_n;i++)
69    {
70        x1[i]=(ll)a[i]*a[i]%mod;
71        x2[i]=(ll)b[i]*b[i]%mod;
72        x3[i]=(ll)a[i]*b[i]%mod;
73        x4[i]=(ll)b[i]*c[i]%mod;
74     }
75     fft(x1,-1);fft(x2,-1);fft(x3,-1);fft(x4,-1);
76     for (int i=1;i<_n;i++)//从1开始,注意边界 
77       x2[i]=((ll)x2[i]+x1[i-1])%mod,x4[i]=((ll)x4[i]+x3[i-1])%mod;
78     if (n&1)
79     {
80         merge(x2,x4,b);
81         for (int i=0;i<_n;i++) a[i]=x4[i];
82     }else 
83       for (int i=0;i<_n;i++) a[i]=x2[i],b[i]=x4[i];
84 }
85 int main()
86 {
87     scanf("%d%d",&n,&k);
88     init(2*k+1); solve(n);
89     for (int i=1;i<=k;i++) printf("%d ",a[i]);
90     puts("");
91    return 0;
92 }
View Code

题解:fft+分治+dp

以上是关于CF755G PolandBall and Many Other Balls的主要内容,如果未能解决你的问题,请参考以下文章

cf755GG. PolandBall and Many Other Balls(dp+生成函数+倍增)

PolandBall and Forest

CodeForces755F PolandBall and Gifts

codeforces D. PolandBall and Polygon

Codeforces 755B???PolandBall and Game???map+?????????

Codeforces 755C:PolandBall and Forest(并查集)