// while (clock()<=69*CLOCKS_PER_SEC) // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (3e6)+100; const int maxk = 20; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; const LL mod = 1e9+7; // ***************************** CODE ***************************** // int tab[maxn], dwa[maxn]; LL suma[maxn]; LL roznica[maxn]; LL res[maxn]; //https://github.com/micgor39/uwr-champions/blob/master/lib/math/fft.cpp //FFT z biblioteczki UWr1 namespace FFT { #define sz(x) SZ(x) typedef long double T; const T PI = acos(-1.0); // typedef C < double > C; struct C { T re, im; C () {} C (T r) : re(r), im(0) {} C (T r, T i) : re(r), im(i) {} C operator * (const C &c) const { return C(re * c.re - im * c.im, im * c.re + re * c.im); } C operator + (const C &c) const { return C(re + c.re, im + c.im); } C operator - (const C &c) const { return C(re - c.re, im - c.im); } void operator += (const C &c) { re += c.re, im += c.im; } C conj() const { return C(re, -im); } }; typedef vector < C > VC; typedef vector < LL > VLL; inline void FFT(C *a, int n, int dir) { for(int i = 0, j = 0; i < n; i++) { if(i > j) swap(a[i], a[j]); for(int k = n >> 1; (j ^= k) < k; k >>= 1); } for(int p = 2; p <= n; p <<= 1) { C wn(cos(2.0 * dir * PI / p), sin(2.0 * dir * PI / p)); for(int k = 0; k < n; k += p) { C w = 1; for(int j = 0; j < (p >> 1); j++) { C xx = a[k + j]; C yy = w * a[k + j + (p >> 1)]; a[k + j] = xx + yy; a[k + j + (p >> 1)] = xx - yy; w = w * wn; } } } } void multiply(VLL &a, VLL &b, VLL &res) { int n = max(a.size(), b.size()), p = 2; while((p >> 1) < n) p <<= 1; C *fa = new C[p + 4]; for(int i = 0; i < p; i++) fa[i] = 0; for(int i = 0; i < sz(a); i++) fa[i] += C(a[i], 0); for(int i = 0; i < sz(b); i++) fa[i] += C(0, b[i]); FFT(fa, p, 1); for(int i = 0; i <= p / 2; i++) { C bp = fa[i] + fa[p - i == p ? 0 : p - i].conj(); C _q = fa[p - i == p ? 0 : p - i] - fa[i].conj(); C q(_q.im, _q.re); fa[i] = (bp * q) * C(0.25); if(i > 0) fa[p - i] = fa[i].conj(); } FFT(fa, p, -1); res.resize(sz(a) + sz(b) - 1); for(int i = 0; i < sz(res); i++) { res[i] = round(fa[i].re / p); } } } void pchaj(int x) { LL ile = min(res[x], res[x + 1]); res[x] -= ile; res[x + 1] -= ile; res[x + 2] += ile; if(ile > 0) { pchaj(x + 1); pchaj(x + 2); } } void solve() { int n; cin>>n; VLL a = {0}, b = {0}; for(int i = 1;i <= n;i++) { cin>>tab[i]; a.pb(tab[i]); } int m; cin>>m; for(int i = 1;i <= m;i++) { cin>>dwa[i]; b.pb(dwa[i]); } VLL c; FFT::multiply(a, b, c); for(int i = 0;i < SZ(c);i++) suma[i] = c[i]; for(int i = 1;i < SZ(a);i += 2) a[i] *= -1; reverse(all(b)); FFT::multiply(a, b, c); for(int i = m;i < SZ(c);i++) roznica[i - m] += c[i] * ((i - m) % 2 == 1 ? -1 : 1); for(int i = m - 1;i >= 0;i--) roznica[m - i] += c[i]; int dlu = n + m + 5; for(int i = dlu;i >= 0;i--) res[i] = suma[i] + roznica[i + 2] - res[i + 2]; res[1] += res[0]; res[0] = 0; for(int i = 1;i <= dlu;i++) { if(res[i] < 0) { res[i + 1] += res[i]; res[i - 1] -= res[i]; res[i] = 0; } res[1] += res[0]; res[0] = 0; } while(1) { for(int i = dlu;i > 0;i--) pchaj(i); for(int i = 1;i <= dlu;i++) { pchaj(i - 1); pchaj(i); if(i == 1) { res[2] += res[1] / 2; res[1] %= 2; } else if(res[i] > 1) { LL ile = res[i] / 2; res[i + 1] += ile; res[max(1, i - 2)] += ile; res[i] -= ile * 2; } } bool ff = 1; for(int i = 1;i <= dlu;i++) if((res[i] > 0 and res[i + 1] > 0) or res[i] > 1) ff = 0; if(ff) break; } int maksi = n + m + 5; while(maksi > 0 and res[maksi] == 0) maksi--; cout<<maksi<<" "; for(int i = 1;i <= maksi;i++) cout<<res[i]<<" "; cout<<"\n"; for(int i = 0;i <= dlu + 10;i++) tab[i] = dwa[i] = suma[i] = roznica[i] = res[i] = 0; } int main() { _upgrade int t; cin>>t; while(t--) solve(); return 0; }
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 | // while (clock()<=69*CLOCKS_PER_SEC) // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (3e6)+100; const int maxk = 20; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; const LL mod = 1e9+7; // ***************************** CODE ***************************** // int tab[maxn], dwa[maxn]; LL suma[maxn]; LL roznica[maxn]; LL res[maxn]; //https://github.com/micgor39/uwr-champions/blob/master/lib/math/fft.cpp //FFT z biblioteczki UWr1 namespace FFT { #define sz(x) SZ(x) typedef long double T; const T PI = acos(-1.0); // typedef C < double > C; struct C { T re, im; C () {} C (T r) : re(r), im(0) {} C (T r, T i) : re(r), im(i) {} C operator * (const C &c) const { return C(re * c.re - im * c.im, im * c.re + re * c.im); } C operator + (const C &c) const { return C(re + c.re, im + c.im); } C operator - (const C &c) const { return C(re - c.re, im - c.im); } void operator += (const C &c) { re += c.re, im += c.im; } C conj() const { return C(re, -im); } }; typedef vector < C > VC; typedef vector < LL > VLL; inline void FFT(C *a, int n, int dir) { for(int i = 0, j = 0; i < n; i++) { if(i > j) swap(a[i], a[j]); for(int k = n >> 1; (j ^= k) < k; k >>= 1); } for(int p = 2; p <= n; p <<= 1) { C wn(cos(2.0 * dir * PI / p), sin(2.0 * dir * PI / p)); for(int k = 0; k < n; k += p) { C w = 1; for(int j = 0; j < (p >> 1); j++) { C xx = a[k + j]; C yy = w * a[k + j + (p >> 1)]; a[k + j] = xx + yy; a[k + j + (p >> 1)] = xx - yy; w = w * wn; } } } } void multiply(VLL &a, VLL &b, VLL &res) { int n = max(a.size(), b.size()), p = 2; while((p >> 1) < n) p <<= 1; C *fa = new C[p + 4]; for(int i = 0; i < p; i++) fa[i] = 0; for(int i = 0; i < sz(a); i++) fa[i] += C(a[i], 0); for(int i = 0; i < sz(b); i++) fa[i] += C(0, b[i]); FFT(fa, p, 1); for(int i = 0; i <= p / 2; i++) { C bp = fa[i] + fa[p - i == p ? 0 : p - i].conj(); C _q = fa[p - i == p ? 0 : p - i] - fa[i].conj(); C q(_q.im, _q.re); fa[i] = (bp * q) * C(0.25); if(i > 0) fa[p - i] = fa[i].conj(); } FFT(fa, p, -1); res.resize(sz(a) + sz(b) - 1); for(int i = 0; i < sz(res); i++) { res[i] = round(fa[i].re / p); } } } void pchaj(int x) { LL ile = min(res[x], res[x + 1]); res[x] -= ile; res[x + 1] -= ile; res[x + 2] += ile; if(ile > 0) { pchaj(x + 1); pchaj(x + 2); } } void solve() { int n; cin>>n; VLL a = {0}, b = {0}; for(int i = 1;i <= n;i++) { cin>>tab[i]; a.pb(tab[i]); } int m; cin>>m; for(int i = 1;i <= m;i++) { cin>>dwa[i]; b.pb(dwa[i]); } VLL c; FFT::multiply(a, b, c); for(int i = 0;i < SZ(c);i++) suma[i] = c[i]; for(int i = 1;i < SZ(a);i += 2) a[i] *= -1; reverse(all(b)); FFT::multiply(a, b, c); for(int i = m;i < SZ(c);i++) roznica[i - m] += c[i] * ((i - m) % 2 == 1 ? -1 : 1); for(int i = m - 1;i >= 0;i--) roznica[m - i] += c[i]; int dlu = n + m + 5; for(int i = dlu;i >= 0;i--) res[i] = suma[i] + roznica[i + 2] - res[i + 2]; res[1] += res[0]; res[0] = 0; for(int i = 1;i <= dlu;i++) { if(res[i] < 0) { res[i + 1] += res[i]; res[i - 1] -= res[i]; res[i] = 0; } res[1] += res[0]; res[0] = 0; } while(1) { for(int i = dlu;i > 0;i--) pchaj(i); for(int i = 1;i <= dlu;i++) { pchaj(i - 1); pchaj(i); if(i == 1) { res[2] += res[1] / 2; res[1] %= 2; } else if(res[i] > 1) { LL ile = res[i] / 2; res[i + 1] += ile; res[max(1, i - 2)] += ile; res[i] -= ile * 2; } } bool ff = 1; for(int i = 1;i <= dlu;i++) if((res[i] > 0 and res[i + 1] > 0) or res[i] > 1) ff = 0; if(ff) break; } int maksi = n + m + 5; while(maksi > 0 and res[maksi] == 0) maksi--; cout<<maksi<<" "; for(int i = 1;i <= maksi;i++) cout<<res[i]<<" "; cout<<"\n"; for(int i = 0;i <= dlu + 10;i++) tab[i] = dwa[i] = suma[i] = roznica[i] = res[i] = 0; } int main() { _upgrade int t; cin>>t; while(t--) solve(); return 0; } |