#include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) using Vll = vector<ll>; //#define PRINT_STUFF //#define PRINT_MORE_STUFF constexpr double PHI = 1.61803398874989484820458683436; constexpr double SQRT_5 = 2.23606797749978969640917366; constexpr int MOD = (119<<23)+1; constexpr int ROOT = 62; ll modPow(ll a, ll e, ll m) { ll t = 1 % m; while (e) { if (e % 2) t = t*a % m; e /= 2; a = a*a % m; } return t; } template<ll M, ll R, bool dit> void ntt(vector<ll>& a) { static vector<ll> w(2, 1); int n = sz(a); for (int k = sz(w); k < n; k *= 2) { w.resize(n, 1); ll c = modPow(R, M/2/k, M); if (dit) c = modPow(c, M-2, M); rep(i, k+1, k*2) w[i] = w[i-1]*c % M; } for (int t = 1; t < n; t *= 2) { int k = (dit ? t : n/t/2); for (int i=0; i < n; i += k*2) rep(j,0,k) { ll &c = a[i+j], &d = a[i+j+k]; ll e = w[j+k], f = d; d = (dit ? c - (f=f*e%M) : (c-f)*e % M); if (d < 0) d += M; if ((c += f) >= M) c -= M; } } } template<ll M = MOD, ll R = ROOT> void convolve(vector<ll>& a, vector<ll> b) { int len = sz(a) + sz(b) - 1; int n = 1 << (32 - __builtin_clz(len)); ll t = modPow(n, M-2, M); a.resize(n); b.resize(n); ntt<M,R,0>(a); ntt<M,R,0>(b); rep(i, 0, n) a[i] = a[i]*b[i] % M * t % M; ntt<M,R,1>(a); a.resize(len); } // --- struct NegVec { Vll elems; int first; NegVec() : first(0) {} NegVec(int start, int end) { init(start, end); } void init(int start, int end) { first = start; elems.assign(end-start, 0); } int lim() const { return sz(elems) + first; } ll operator[](int i) const { return elems[i-first]; } ll& operator[](int i) { return elems[i-first]; } }; void printPhinary(const char* name, const NegVec& repr, double scale) { double val = 0; rep(i, repr.first, repr.lim()) { val += double(repr[i]) * pow(PHI, i); } val /= scale; cerr << name << ": " << val << " in phi"; #ifdef PRINT_MORE_STUFF cerr << ": "; for (int i = repr.lim()-1; i >= 0; i--) { cerr << repr[i] << ' '; } cerr << ". "; for (int i = -1; i >= repr.first; i--) { cerr << repr[i] << ' '; } #endif cerr << '\n'; } void printFibo(const char* name, const Vll& repr) { ll f1 = 1, f2 = 2, val = 0; each(c, repr) { val += c*f1; tie(f1, f2) = mp(f2, f1+f2); } cerr << name << ": " << double(val) << " in fib"; #ifdef PRINT_MORE_STUFF cerr << ": "; for (int i = sz(repr)-1; i >= 0; i--) { cerr << repr[i] << ' '; } #endif cerr << '\n'; } // --- NegVec mulPhinary(const NegVec& A, const NegVec& B) { auto poly1 = A.elems, poly2 = B.elems; convolve(poly1, poly2); NegVec ret; ret.elems = poly1; ret.first = A.first + B.first; each(e, ret.elems) if (e >= MOD/2) e -= MOD; return ret; } void divBySqrt5(NegVec& A) { for (int i = A.lim()-1; i >= 4; i--) { A[i-4] += A[i]; } A.first -= 2; A[2] += A[0]; A[0] = 0; rep(i, A.first, 0) { A[i] = (-i < A.lim() ? A[-i] : 0) * (i % 2 ? 1 : -1); } } NegVec toPhinary(const Vll& repr) { NegVec ret(-sz(repr)-2, sz(repr)+2); rep(i, 0, sz(repr)) { ret[i+2] += repr[i]; ret[-i-2] += repr[i] * (i % 2 ? 1 : -1); } return ret; } Vll toFibo(const NegVec& repr) { Vll ret(repr.lim()-2); rep(i, 0, sz(ret)) ret[i] = repr[i+2]; if (repr.lim() > 1) ret[0] += repr[1]; return ret; } void trim(Vll& vec) { while (!vec.empty() && vec.back() == 0) { vec.pop_back(); } } // --- void standarize2(Vll& repr) { trim(repr); repr.resize(max(sz(repr)+1, 5)); // Remove twos auto is3 = [&](int i, ll a, ll b, ll c) { return repr[i] == a && repr[i+1] == b && repr[i+2] == c; }; auto set4 = [&](int i, ll a, ll b, ll c, ll d) { repr[i] = a; repr[i+1] = b; repr[i+2] = c; repr[i+3] = d; }; for (int i = sz(repr)-4; i >= 0; i--) { if (is3(i+1, 0, 2, 0)) { set4(i, repr[i]+1, 0, 0, 1); } else if (is3(i+1, 0, 3, 0)) { set4(i, repr[i]+1, 0, 1, 1); } else if (is3(i+1, 1, 2, 0)) { set4(i, repr[i], 0, 1, 1); } else if (is3(i+1, 2, 1, 0)) { set4(i, repr[i], 1, 0, 1); } } if (repr[1] == 3) { // 030 -> 111 repr[0] = repr[1] = repr[2] = 1; } else if (repr[1] == 2) { if (repr[2] == 0) { // 02x -> 10y repr[0]++; repr[1] = 0; repr[2] = 1; } else { // 0120 -> 1010 repr[1] = 1; repr[2] = 0; repr[3] = 1; } } if (repr[0] == 3) { // 03 -> 11 repr[0] = repr[1] = 1; } else if (repr[0] == 2) { if (repr[1] == 0) { // 02 -> 10 repr[0] = 0; repr[1] = 1; } else { // 012 -> 101 repr[0] = 1; repr[1] = 0; repr[2] = 1; } } // Remove consecutive ones repr.pb(0); rep(i, 0, sz(repr)-2) { if (is3(i, 1, 1, 0)) { // 011 -> 100 repr[i] = repr[i+1] = 0; repr[i+2] = 1; } } repr.pb(0); for (int i = sz(repr)-3; i >= 0; i--) { if (is3(i, 1, 1, 0)) { // 011 -> 100 repr[i] = repr[i+1] = 0; repr[i+2] = 1; } } trim(repr); } void fiboAdd(Vll& dst, Vll& src) { if (sz(src) > sz(dst)) dst.resize(sz(src)); rep(i, 0, sz(src)) dst[i] += src[i]; standarize2(dst); } void standarize(Vll& repr) { ll great = 0; each(k, repr) great = max(great, k); if (great <= 1) { standarize2(repr); return; } Vll even(sz(repr)); rep(i, 0, sz(repr)) { even[i] = repr[i] / 2; repr[i] %= 2; } standarize2(repr); standarize(even); fiboAdd(even, even); fiboAdd(repr, even); } // --- Vll readVec() { int n; cin >> n; Vll ret(n); each(k, ret) cin >> k; return ret; } void run() { Vll A = readVec(); Vll B = readVec(); NegVec pA = toPhinary(A); NegVec pB = toPhinary(B); #ifdef PRINT_STUFF printFibo("A", A); printPhinary("A", pA, SQRT_5); printFibo("B", B); printPhinary("B", pB, SQRT_5); #endif NegVec pC = mulPhinary(pA, pB); #ifdef PRINT_STUFF printPhinary("C", pC, 5); #endif divBySqrt5(pC); #ifdef PRINT_STUFF printPhinary("C", pC, SQRT_5); #endif Vll C = toFibo(pC); #ifdef PRINT_STUFF printFibo("C", C); #endif standarize(C); #ifdef PRINT_STUFF printFibo("C", C); #endif trim(C); cout << sz(C) << ' '; each(k, C) cout << k << ' '; cout << '\n'; #ifdef PRINT_STUFF cerr << '\n'; #endif } int main() { cin.sync_with_stdio(0); cin.tie(0); cerr << fixed << setprecision(5); int t; cin >> t; while (t--) run(); 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 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 347 348 349 350 351 352 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) using Vll = vector<ll>; //#define PRINT_STUFF //#define PRINT_MORE_STUFF constexpr double PHI = 1.61803398874989484820458683436; constexpr double SQRT_5 = 2.23606797749978969640917366; constexpr int MOD = (119<<23)+1; constexpr int ROOT = 62; ll modPow(ll a, ll e, ll m) { ll t = 1 % m; while (e) { if (e % 2) t = t*a % m; e /= 2; a = a*a % m; } return t; } template<ll M, ll R, bool dit> void ntt(vector<ll>& a) { static vector<ll> w(2, 1); int n = sz(a); for (int k = sz(w); k < n; k *= 2) { w.resize(n, 1); ll c = modPow(R, M/2/k, M); if (dit) c = modPow(c, M-2, M); rep(i, k+1, k*2) w[i] = w[i-1]*c % M; } for (int t = 1; t < n; t *= 2) { int k = (dit ? t : n/t/2); for (int i=0; i < n; i += k*2) rep(j,0,k) { ll &c = a[i+j], &d = a[i+j+k]; ll e = w[j+k], f = d; d = (dit ? c - (f=f*e%M) : (c-f)*e % M); if (d < 0) d += M; if ((c += f) >= M) c -= M; } } } template<ll M = MOD, ll R = ROOT> void convolve(vector<ll>& a, vector<ll> b) { int len = sz(a) + sz(b) - 1; int n = 1 << (32 - __builtin_clz(len)); ll t = modPow(n, M-2, M); a.resize(n); b.resize(n); ntt<M,R,0>(a); ntt<M,R,0>(b); rep(i, 0, n) a[i] = a[i]*b[i] % M * t % M; ntt<M,R,1>(a); a.resize(len); } // --- struct NegVec { Vll elems; int first; NegVec() : first(0) {} NegVec(int start, int end) { init(start, end); } void init(int start, int end) { first = start; elems.assign(end-start, 0); } int lim() const { return sz(elems) + first; } ll operator[](int i) const { return elems[i-first]; } ll& operator[](int i) { return elems[i-first]; } }; void printPhinary(const char* name, const NegVec& repr, double scale) { double val = 0; rep(i, repr.first, repr.lim()) { val += double(repr[i]) * pow(PHI, i); } val /= scale; cerr << name << ": " << val << " in phi"; #ifdef PRINT_MORE_STUFF cerr << ": "; for (int i = repr.lim()-1; i >= 0; i--) { cerr << repr[i] << ' '; } cerr << ". "; for (int i = -1; i >= repr.first; i--) { cerr << repr[i] << ' '; } #endif cerr << '\n'; } void printFibo(const char* name, const Vll& repr) { ll f1 = 1, f2 = 2, val = 0; each(c, repr) { val += c*f1; tie(f1, f2) = mp(f2, f1+f2); } cerr << name << ": " << double(val) << " in fib"; #ifdef PRINT_MORE_STUFF cerr << ": "; for (int i = sz(repr)-1; i >= 0; i--) { cerr << repr[i] << ' '; } #endif cerr << '\n'; } // --- NegVec mulPhinary(const NegVec& A, const NegVec& B) { auto poly1 = A.elems, poly2 = B.elems; convolve(poly1, poly2); NegVec ret; ret.elems = poly1; ret.first = A.first + B.first; each(e, ret.elems) if (e >= MOD/2) e -= MOD; return ret; } void divBySqrt5(NegVec& A) { for (int i = A.lim()-1; i >= 4; i--) { A[i-4] += A[i]; } A.first -= 2; A[2] += A[0]; A[0] = 0; rep(i, A.first, 0) { A[i] = (-i < A.lim() ? A[-i] : 0) * (i % 2 ? 1 : -1); } } NegVec toPhinary(const Vll& repr) { NegVec ret(-sz(repr)-2, sz(repr)+2); rep(i, 0, sz(repr)) { ret[i+2] += repr[i]; ret[-i-2] += repr[i] * (i % 2 ? 1 : -1); } return ret; } Vll toFibo(const NegVec& repr) { Vll ret(repr.lim()-2); rep(i, 0, sz(ret)) ret[i] = repr[i+2]; if (repr.lim() > 1) ret[0] += repr[1]; return ret; } void trim(Vll& vec) { while (!vec.empty() && vec.back() == 0) { vec.pop_back(); } } // --- void standarize2(Vll& repr) { trim(repr); repr.resize(max(sz(repr)+1, 5)); // Remove twos auto is3 = [&](int i, ll a, ll b, ll c) { return repr[i] == a && repr[i+1] == b && repr[i+2] == c; }; auto set4 = [&](int i, ll a, ll b, ll c, ll d) { repr[i] = a; repr[i+1] = b; repr[i+2] = c; repr[i+3] = d; }; for (int i = sz(repr)-4; i >= 0; i--) { if (is3(i+1, 0, 2, 0)) { set4(i, repr[i]+1, 0, 0, 1); } else if (is3(i+1, 0, 3, 0)) { set4(i, repr[i]+1, 0, 1, 1); } else if (is3(i+1, 1, 2, 0)) { set4(i, repr[i], 0, 1, 1); } else if (is3(i+1, 2, 1, 0)) { set4(i, repr[i], 1, 0, 1); } } if (repr[1] == 3) { // 030 -> 111 repr[0] = repr[1] = repr[2] = 1; } else if (repr[1] == 2) { if (repr[2] == 0) { // 02x -> 10y repr[0]++; repr[1] = 0; repr[2] = 1; } else { // 0120 -> 1010 repr[1] = 1; repr[2] = 0; repr[3] = 1; } } if (repr[0] == 3) { // 03 -> 11 repr[0] = repr[1] = 1; } else if (repr[0] == 2) { if (repr[1] == 0) { // 02 -> 10 repr[0] = 0; repr[1] = 1; } else { // 012 -> 101 repr[0] = 1; repr[1] = 0; repr[2] = 1; } } // Remove consecutive ones repr.pb(0); rep(i, 0, sz(repr)-2) { if (is3(i, 1, 1, 0)) { // 011 -> 100 repr[i] = repr[i+1] = 0; repr[i+2] = 1; } } repr.pb(0); for (int i = sz(repr)-3; i >= 0; i--) { if (is3(i, 1, 1, 0)) { // 011 -> 100 repr[i] = repr[i+1] = 0; repr[i+2] = 1; } } trim(repr); } void fiboAdd(Vll& dst, Vll& src) { if (sz(src) > sz(dst)) dst.resize(sz(src)); rep(i, 0, sz(src)) dst[i] += src[i]; standarize2(dst); } void standarize(Vll& repr) { ll great = 0; each(k, repr) great = max(great, k); if (great <= 1) { standarize2(repr); return; } Vll even(sz(repr)); rep(i, 0, sz(repr)) { even[i] = repr[i] / 2; repr[i] %= 2; } standarize2(repr); standarize(even); fiboAdd(even, even); fiboAdd(repr, even); } // --- Vll readVec() { int n; cin >> n; Vll ret(n); each(k, ret) cin >> k; return ret; } void run() { Vll A = readVec(); Vll B = readVec(); NegVec pA = toPhinary(A); NegVec pB = toPhinary(B); #ifdef PRINT_STUFF printFibo("A", A); printPhinary("A", pA, SQRT_5); printFibo("B", B); printPhinary("B", pB, SQRT_5); #endif NegVec pC = mulPhinary(pA, pB); #ifdef PRINT_STUFF printPhinary("C", pC, 5); #endif divBySqrt5(pC); #ifdef PRINT_STUFF printPhinary("C", pC, SQRT_5); #endif Vll C = toFibo(pC); #ifdef PRINT_STUFF printFibo("C", C); #endif standarize(C); #ifdef PRINT_STUFF printFibo("C", C); #endif trim(C); cout << sz(C) << ' '; each(k, C) cout << k << ' '; cout << '\n'; #ifdef PRINT_STUFF cerr << '\n'; #endif } int main() { cin.sync_with_stdio(0); cin.tie(0); cerr << fixed << setprecision(5); int t; cin >> t; while (t--) run(); return 0; } |