#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=2010000; int f[N],g[N],n,m,_; ll pc[N],ps[N]; // FFT_MAXN = 2^k // fft_init() to precalc FFT_MAXN-th roots //typedef double db; const int FFT_MAXN=1048576; const db pi=acos(-1.); struct cp{ db a,b; cp operator+(const cp&y)const{return (cp){a+y.a,b+y.b};} cp operator-(const cp&y)const{return (cp){a-y.a,b-y.b};} cp operator*(const cp&y)const{return (cp){a*y.a-b*y.b,a*y.b+b*y.a};} cp operator!()const{return (cp){a,-b};}; }nw[FFT_MAXN+1];int bitrev[FFT_MAXN]; void dft(cp*a,int n,int flag=1){ int d=0;while((1<<d)*n!=FFT_MAXN)d++; rep(i,0,n)if(i<(bitrev[i]>>d))swap(a[i],a[bitrev[i]>>d]); for (int l=2;l<=n;l<<=1){ int del=FFT_MAXN/l*flag; for (int i=0;i<n;i+=l){ cp *le=a+i,*ri=a+i+(l>>1),*w=flag==1?nw:nw+FFT_MAXN; rep(k,0,l>>1){ cp ne=*ri**w; *ri=*le-ne,*le=*le+ne; le++,ri++,w+=del; } } } if(flag!=1)rep(i,0,n)a[i].a/=n,a[i].b/=n; } void fft_init(){ int L=0;while((1<<L)!=FFT_MAXN)L++; bitrev[0]=0;rep(i,1,FFT_MAXN)bitrev[i]=bitrev[i>>1]>>1|((i&1)<<(L-1)); nw[0]=nw[FFT_MAXN]=(cp){1,0}; rep(i,0,FFT_MAXN+1)nw[i]=(cp){cos(2*pi/FFT_MAXN*i),sin(2*pi/FFT_MAXN*i)}; //very slow } db pf[N],pg[N],cc[N]; void convo(db*a,int n,db*b,int m,db*c){ static cp f[FFT_MAXN>>1],g[FFT_MAXN>>1],t[FFT_MAXN>>1]; int N=2;while(N<=n+m)N<<=1; rep(i,0,N) if(i&1){ f[i>>1].b=(i<=n)?a[i]:0.0; g[i>>1].b=(i<=m)?b[i]:0.0; }else{ f[i>>1].a=(i<=n)?a[i]:0.0; g[i>>1].a=(i<=m)?b[i]:0.0; } dft(f,N>>1);dft(g,N>>1); int del=FFT_MAXN/(N>>1); cp qua=(cp){0,0.25},one=(cp){1,0},four=(cp){4,0},*w=nw; rep(i,0,N>>1){ int j=i?(N>>1)-i:0; t[i]=(four*!(f[j]*g[j])-(!f[j]-f[i])*(!g[j]-g[i])*(one+*w))*qua; w+=del; } dft(t,N>>1,-1); rep(i,0,n+m+1)c[i]=(i&1)?t[i>>1].a:t[i>>1].b; } struct fibnum { int maxb; char dat[N]; void init() { for (int i=0;i<=maxb;i++) dat[i]=0; maxb=0; } }ans,zz; int tmp[N]; void dec(int x) { if (tmp[x]<2) return; tmp[x]-=2; tmp[x+1]+=1; if (x>=1) tmp[max(x-2,0)]++; } void up(int x) { while (tmp[x]) { tmp[x]--; tmp[x-1]--; tmp[x+1]++; x+=2; } } void clear(int &len) { tmp[len+1]=tmp[len+2]=tmp[len+3]=tmp[len+4]=tmp[len+5]=0; //rep(i,0,len+1) printf("%d",tmp[i]); puts(""); for (int i=len+1;i>=3;i--){ if (tmp[i]!=0) continue; if (tmp[i-1]==2) { if (tmp[i-2]==0) tmp[i]=1,tmp[i-1]=0,tmp[i-2]=0,tmp[i-3]+=1; if (tmp[i-2]==1) tmp[i]=1,tmp[i-1]=1,tmp[i-2]=0; } else if (tmp[i-1]==3) { if (tmp[i-2]==0) tmp[i]=1,tmp[i-1]=1,tmp[i-2]=0,tmp[i-3]+=1; } else if (tmp[i-1]==1&&tmp[i-2]==2) { tmp[i]=1; tmp[i-1]=0; tmp[i-2]=1; } } if (tmp[1]==3) { assert(tmp[2]==0&&tmp[0]==0); tmp[1]=1; tmp[2]=1; tmp[0]=1; } if (tmp[1]==2) { if (tmp[2]==0) { //assert(tmp[0]==0); tmp[2]=1; tmp[1]=0; tmp[0]++; } else { assert(tmp[2]==1&&tmp[3]==0&&tmp[0]==0); tmp[3]=1; tmp[2]=0; tmp[1]=1; } } if (tmp[0]==3) { assert(tmp[1]==0); tmp[1]=1; tmp[0]=1; } if (tmp[0]==2) { if (tmp[1]==0) { tmp[1]=1; tmp[0]=0; } else { assert(tmp[1]==1&&tmp[2]==0); tmp[2]=1; tmp[1]=0; tmp[0]=1; } } while (tmp[len+1]==1) len++; for (int i=0;i<=len;i++) assert(tmp[i]<=1); for (int i=0;i<=len;i++) if (tmp[i]==1&&tmp[i+1]==1&&tmp[i+2]==0) { tmp[i]=0; tmp[i+1]=0; tmp[i+2]=1; } while (tmp[len+1]==1) len++; for (int i=len;i>=0;i--) if (tmp[i]==1&&tmp[i+1]==1&&tmp[i+2]==0) { tmp[i]=0; tmp[i+1]=0; tmp[i+2]=1; } while (tmp[len+1]==1) len++; assert(len==0||tmp[len]==1); for (int i=0;i<len;i++) assert(!tmp[i]||!tmp[i+1]); } void add(fibnum &a,fibnum &b) { int len=max(a.maxb,b.maxb); for (int i=0;i<=len;i++) { tmp[i]=0; if (i<=a.maxb) tmp[i]+=a.dat[i]; if (i<=b.maxb) tmp[i]+=b.dat[i]; } clear(len); a.maxb=len; for (int i=0;i<=len;i++) a.dat[i]=tmp[i]; } void solve() { scanf("%d",&n); for (int i=0;i<n;i++) scanf("%d",f+i); scanf("%d",&m); for (int i=0;i<m;i++) scanf("%d",g+i); for (int i=0;i<=n+m-2;i++) pc[i]=0,ps[i]=0; for (int i=0;i<n;i++) pf[i]=f[i]; for (int i=0;i<m;i++) pg[i]=g[i]; for (int i=1;i<n&&i<m;i+=2) if (f[i]&&g[i]) pc[0]++; convo(pf,n-1,pg,m-1,cc); rep(i,0,n+m-1) ps[i]=(int)(cc[i]+0.1); for (int r1=0;r1<=1;r1++) for (int r2=0;r2<=1;r2++) { int pn=0,pm=0; for (int i=r1;i<n;i+=2) pf[pn++]=f[i]; for (int i=r2;i<m;i+=2) pg[pm++]=g[i]; if (pn==0||pm==0) continue; reverse(pg,pg+pm); convo(pf,pn-1,pg,pm-1,cc); for (int i=0;i<=pn+pm-2;i++) { int act=2*(i-(pm-1))+r1-r2; int p=act>0?r2:r1; int val=(int)(cc[i]+0.1); //printf("gg %d\n",act); if (p==0) { if (act>=4) ps[act-4]-=val; else if (act<=-4) ps[-act-4]-=val; } else { if (act>=2) ps[act-2]-=val; else if (act<=-2) ps[-act-2]-=val; if (act>=1) pc[act-1]+=val; else if (act<=-1) pc[-act-1]+=val; } } } for (int i=n+m-2;i>=0;i--) { pc[i]+=ps[i]; if (i>=4) ps[i-4]+=ps[i]; } ans.init(); for (int i=40;i>=0;i--) { add(ans,ans); for (int j=0;j<=n+m-2;j++) tmp[j]=0; int len=0; for (int j=0;j<=n+m-2;j++) if (pc[j]&(1ll<<i)) len=j,tmp[j]=1; clear(len); zz.maxb=len; rep(j,0,len+1) zz.dat[j]=tmp[j]; add(ans,zz); } printf("%d",ans.maxb+1); rep(i,0,ans.maxb+1) printf(" %d",(int)ans.dat[i]); puts(""); } int main() { fft_init(); for (scanf("%d",&_);_;_--) { solve(); } }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=2010000; int f[N],g[N],n,m,_; ll pc[N],ps[N]; // FFT_MAXN = 2^k // fft_init() to precalc FFT_MAXN-th roots //typedef double db; const int FFT_MAXN=1048576; const db pi=acos(-1.); struct cp{ db a,b; cp operator+(const cp&y)const{return (cp){a+y.a,b+y.b};} cp operator-(const cp&y)const{return (cp){a-y.a,b-y.b};} cp operator*(const cp&y)const{return (cp){a*y.a-b*y.b,a*y.b+b*y.a};} cp operator!()const{return (cp){a,-b};}; }nw[FFT_MAXN+1];int bitrev[FFT_MAXN]; void dft(cp*a,int n,int flag=1){ int d=0;while((1<<d)*n!=FFT_MAXN)d++; rep(i,0,n)if(i<(bitrev[i]>>d))swap(a[i],a[bitrev[i]>>d]); for (int l=2;l<=n;l<<=1){ int del=FFT_MAXN/l*flag; for (int i=0;i<n;i+=l){ cp *le=a+i,*ri=a+i+(l>>1),*w=flag==1?nw:nw+FFT_MAXN; rep(k,0,l>>1){ cp ne=*ri**w; *ri=*le-ne,*le=*le+ne; le++,ri++,w+=del; } } } if(flag!=1)rep(i,0,n)a[i].a/=n,a[i].b/=n; } void fft_init(){ int L=0;while((1<<L)!=FFT_MAXN)L++; bitrev[0]=0;rep(i,1,FFT_MAXN)bitrev[i]=bitrev[i>>1]>>1|((i&1)<<(L-1)); nw[0]=nw[FFT_MAXN]=(cp){1,0}; rep(i,0,FFT_MAXN+1)nw[i]=(cp){cos(2*pi/FFT_MAXN*i),sin(2*pi/FFT_MAXN*i)}; //very slow } db pf[N],pg[N],cc[N]; void convo(db*a,int n,db*b,int m,db*c){ static cp f[FFT_MAXN>>1],g[FFT_MAXN>>1],t[FFT_MAXN>>1]; int N=2;while(N<=n+m)N<<=1; rep(i,0,N) if(i&1){ f[i>>1].b=(i<=n)?a[i]:0.0; g[i>>1].b=(i<=m)?b[i]:0.0; }else{ f[i>>1].a=(i<=n)?a[i]:0.0; g[i>>1].a=(i<=m)?b[i]:0.0; } dft(f,N>>1);dft(g,N>>1); int del=FFT_MAXN/(N>>1); cp qua=(cp){0,0.25},one=(cp){1,0},four=(cp){4,0},*w=nw; rep(i,0,N>>1){ int j=i?(N>>1)-i:0; t[i]=(four*!(f[j]*g[j])-(!f[j]-f[i])*(!g[j]-g[i])*(one+*w))*qua; w+=del; } dft(t,N>>1,-1); rep(i,0,n+m+1)c[i]=(i&1)?t[i>>1].a:t[i>>1].b; } struct fibnum { int maxb; char dat[N]; void init() { for (int i=0;i<=maxb;i++) dat[i]=0; maxb=0; } }ans,zz; int tmp[N]; void dec(int x) { if (tmp[x]<2) return; tmp[x]-=2; tmp[x+1]+=1; if (x>=1) tmp[max(x-2,0)]++; } void up(int x) { while (tmp[x]) { tmp[x]--; tmp[x-1]--; tmp[x+1]++; x+=2; } } void clear(int &len) { tmp[len+1]=tmp[len+2]=tmp[len+3]=tmp[len+4]=tmp[len+5]=0; //rep(i,0,len+1) printf("%d",tmp[i]); puts(""); for (int i=len+1;i>=3;i--){ if (tmp[i]!=0) continue; if (tmp[i-1]==2) { if (tmp[i-2]==0) tmp[i]=1,tmp[i-1]=0,tmp[i-2]=0,tmp[i-3]+=1; if (tmp[i-2]==1) tmp[i]=1,tmp[i-1]=1,tmp[i-2]=0; } else if (tmp[i-1]==3) { if (tmp[i-2]==0) tmp[i]=1,tmp[i-1]=1,tmp[i-2]=0,tmp[i-3]+=1; } else if (tmp[i-1]==1&&tmp[i-2]==2) { tmp[i]=1; tmp[i-1]=0; tmp[i-2]=1; } } if (tmp[1]==3) { assert(tmp[2]==0&&tmp[0]==0); tmp[1]=1; tmp[2]=1; tmp[0]=1; } if (tmp[1]==2) { if (tmp[2]==0) { //assert(tmp[0]==0); tmp[2]=1; tmp[1]=0; tmp[0]++; } else { assert(tmp[2]==1&&tmp[3]==0&&tmp[0]==0); tmp[3]=1; tmp[2]=0; tmp[1]=1; } } if (tmp[0]==3) { assert(tmp[1]==0); tmp[1]=1; tmp[0]=1; } if (tmp[0]==2) { if (tmp[1]==0) { tmp[1]=1; tmp[0]=0; } else { assert(tmp[1]==1&&tmp[2]==0); tmp[2]=1; tmp[1]=0; tmp[0]=1; } } while (tmp[len+1]==1) len++; for (int i=0;i<=len;i++) assert(tmp[i]<=1); for (int i=0;i<=len;i++) if (tmp[i]==1&&tmp[i+1]==1&&tmp[i+2]==0) { tmp[i]=0; tmp[i+1]=0; tmp[i+2]=1; } while (tmp[len+1]==1) len++; for (int i=len;i>=0;i--) if (tmp[i]==1&&tmp[i+1]==1&&tmp[i+2]==0) { tmp[i]=0; tmp[i+1]=0; tmp[i+2]=1; } while (tmp[len+1]==1) len++; assert(len==0||tmp[len]==1); for (int i=0;i<len;i++) assert(!tmp[i]||!tmp[i+1]); } void add(fibnum &a,fibnum &b) { int len=max(a.maxb,b.maxb); for (int i=0;i<=len;i++) { tmp[i]=0; if (i<=a.maxb) tmp[i]+=a.dat[i]; if (i<=b.maxb) tmp[i]+=b.dat[i]; } clear(len); a.maxb=len; for (int i=0;i<=len;i++) a.dat[i]=tmp[i]; } void solve() { scanf("%d",&n); for (int i=0;i<n;i++) scanf("%d",f+i); scanf("%d",&m); for (int i=0;i<m;i++) scanf("%d",g+i); for (int i=0;i<=n+m-2;i++) pc[i]=0,ps[i]=0; for (int i=0;i<n;i++) pf[i]=f[i]; for (int i=0;i<m;i++) pg[i]=g[i]; for (int i=1;i<n&&i<m;i+=2) if (f[i]&&g[i]) pc[0]++; convo(pf,n-1,pg,m-1,cc); rep(i,0,n+m-1) ps[i]=(int)(cc[i]+0.1); for (int r1=0;r1<=1;r1++) for (int r2=0;r2<=1;r2++) { int pn=0,pm=0; for (int i=r1;i<n;i+=2) pf[pn++]=f[i]; for (int i=r2;i<m;i+=2) pg[pm++]=g[i]; if (pn==0||pm==0) continue; reverse(pg,pg+pm); convo(pf,pn-1,pg,pm-1,cc); for (int i=0;i<=pn+pm-2;i++) { int act=2*(i-(pm-1))+r1-r2; int p=act>0?r2:r1; int val=(int)(cc[i]+0.1); //printf("gg %d\n",act); if (p==0) { if (act>=4) ps[act-4]-=val; else if (act<=-4) ps[-act-4]-=val; } else { if (act>=2) ps[act-2]-=val; else if (act<=-2) ps[-act-2]-=val; if (act>=1) pc[act-1]+=val; else if (act<=-1) pc[-act-1]+=val; } } } for (int i=n+m-2;i>=0;i--) { pc[i]+=ps[i]; if (i>=4) ps[i-4]+=ps[i]; } ans.init(); for (int i=40;i>=0;i--) { add(ans,ans); for (int j=0;j<=n+m-2;j++) tmp[j]=0; int len=0; for (int j=0;j<=n+m-2;j++) if (pc[j]&(1ll<<i)) len=j,tmp[j]=1; clear(len); zz.maxb=len; rep(j,0,len+1) zz.dat[j]=tmp[j]; add(ans,zz); } printf("%d",ans.maxb+1); rep(i,0,ans.maxb+1) printf(" %d",(int)ans.dat[i]); puts(""); } int main() { fft_init(); for (scanf("%d",&_);_;_--) { solve(); } } |