#include <cstdio> #include <cstdlib> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> using namespace std; typedef long long ll; typedef pair<int, int> PII; int read() { int n; scanf("%d", &n); return n; } struct BI { static const int BASE = 1 << 30; vector<int> data; BI(int n = 0) { while (n > 0) { data.push_back(n % BASE); n /= BASE; } } void add(const BI &oth) { if (data.size() < oth.data.size()) data.resize(oth.data.size(), 0); for (int i=0; i<oth.data.size(); ++i) data[i] += oth.data[i]; for (int i=0; i<data.size(); ++i) if (data[i] >= BASE) { data[i] -= BASE; if (i + 1 == data.size()) data.push_back(0); ++data[i+1]; } } void sub(const BI &oth) { for (int i=0; i<oth.data.size(); ++i) { data[i] -= oth.data[i]; if (data[i] < 0) { data[i] += BASE; data[i+1]--; } } while (!data.empty() && data.back() == 0) data.pop_back(); } void mul(const BI &oth) { vector<int> res(data.size() + oth.data.size() + 2, 0); vector<ll> prz(data.size() + oth.data.size() + 2, 0); for (int i=0; i<data.size(); ++i) { for (int j=0; j<oth.data.size(); ++j) { ll cur = 1LL * data[i] * oth.data[j]; ll div = cur / BASE; prz[i+j] += div; cur -= div * BASE; res[i+j] += cur; if (res[i+j] >= BASE) { prz[i+j]++; res[i+j] -= BASE; } } } for (int i=0; i<prz.size(); ++i) if (prz[i] >= BASE) { ll div = prz[i] / BASE; prz[i+1] += div; prz[i] -= div * BASE; } for (int i=1; i<res.size(); ++i) { res[i] += prz[i-1]; if (res[i] >= BASE) { res[i] -= BASE; res[i+1]++; } } while (!res.empty() && res.back() == 0) res.pop_back(); swap(data, res); } bool less(const BI &oth) { if (data.size() != oth.data.size()) return data.size() < oth.data.size(); for (int i=data.size()-1; i>=0; --i) if (data[i] != oth.data[i]) return data[i] < oth.data[i]; return false; } bool isZero() { return data.empty(); } void show() { for (int i=data.size() - 1; i >= 0; --i) printf("%d", data[i]); printf("\n"); } }; vector<BI> fib; void solve() { int n1 = read(); while (n1 >= fib.size()) { fib.push_back(fib.back()); fib.back().add(fib[fib.size() - 3]); } BI a; for (int i=0; i<n1; ++i) { if (read()) a.add(fib[i]); } int n2 = read(); while (n2 >= fib.size()) { fib.push_back(fib.back()); fib.back().add(fib[fib.size() - 3]); } BI b; for (int i=0; i<n2; ++i) { if (read()) b.add(fib[i]); } a.mul(b); while (fib.back().less(a)) { fib.push_back(fib.back()); fib.back().add(fib[fib.size() - 3]); } vector<int> res; bool isOne = false; for (int i=fib.size() - 1; i>=0; --i) { if (!(a.less(fib[i]))) { isOne = true; res.push_back(1); if (i > 0) res.push_back(0); a.sub(fib[i]); --i; } else { if (isOne) res.push_back(0); } } printf("%d", (int)res.size()); for (int i=res.size()-1; i>=0; --i) printf(" %d", res[i]); printf("\n"); } int main() { fib.push_back(BI(1)); fib.push_back(BI(2)); int t = read(); 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 | #include <cstdio> #include <cstdlib> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> using namespace std; typedef long long ll; typedef pair<int, int> PII; int read() { int n; scanf("%d", &n); return n; } struct BI { static const int BASE = 1 << 30; vector<int> data; BI(int n = 0) { while (n > 0) { data.push_back(n % BASE); n /= BASE; } } void add(const BI &oth) { if (data.size() < oth.data.size()) data.resize(oth.data.size(), 0); for (int i=0; i<oth.data.size(); ++i) data[i] += oth.data[i]; for (int i=0; i<data.size(); ++i) if (data[i] >= BASE) { data[i] -= BASE; if (i + 1 == data.size()) data.push_back(0); ++data[i+1]; } } void sub(const BI &oth) { for (int i=0; i<oth.data.size(); ++i) { data[i] -= oth.data[i]; if (data[i] < 0) { data[i] += BASE; data[i+1]--; } } while (!data.empty() && data.back() == 0) data.pop_back(); } void mul(const BI &oth) { vector<int> res(data.size() + oth.data.size() + 2, 0); vector<ll> prz(data.size() + oth.data.size() + 2, 0); for (int i=0; i<data.size(); ++i) { for (int j=0; j<oth.data.size(); ++j) { ll cur = 1LL * data[i] * oth.data[j]; ll div = cur / BASE; prz[i+j] += div; cur -= div * BASE; res[i+j] += cur; if (res[i+j] >= BASE) { prz[i+j]++; res[i+j] -= BASE; } } } for (int i=0; i<prz.size(); ++i) if (prz[i] >= BASE) { ll div = prz[i] / BASE; prz[i+1] += div; prz[i] -= div * BASE; } for (int i=1; i<res.size(); ++i) { res[i] += prz[i-1]; if (res[i] >= BASE) { res[i] -= BASE; res[i+1]++; } } while (!res.empty() && res.back() == 0) res.pop_back(); swap(data, res); } bool less(const BI &oth) { if (data.size() != oth.data.size()) return data.size() < oth.data.size(); for (int i=data.size()-1; i>=0; --i) if (data[i] != oth.data[i]) return data[i] < oth.data[i]; return false; } bool isZero() { return data.empty(); } void show() { for (int i=data.size() - 1; i >= 0; --i) printf("%d", data[i]); printf("\n"); } }; vector<BI> fib; void solve() { int n1 = read(); while (n1 >= fib.size()) { fib.push_back(fib.back()); fib.back().add(fib[fib.size() - 3]); } BI a; for (int i=0; i<n1; ++i) { if (read()) a.add(fib[i]); } int n2 = read(); while (n2 >= fib.size()) { fib.push_back(fib.back()); fib.back().add(fib[fib.size() - 3]); } BI b; for (int i=0; i<n2; ++i) { if (read()) b.add(fib[i]); } a.mul(b); while (fib.back().less(a)) { fib.push_back(fib.back()); fib.back().add(fib[fib.size() - 3]); } vector<int> res; bool isOne = false; for (int i=fib.size() - 1; i>=0; --i) { if (!(a.less(fib[i]))) { isOne = true; res.push_back(1); if (i > 0) res.push_back(0); a.sub(fib[i]); --i; } else { if (isOne) res.push_back(0); } } printf("%d", (int)res.size()); for (int i=res.size()-1; i>=0; --i) printf(" %d", res[i]); printf("\n"); } int main() { fib.push_back(BI(1)); fib.push_back(BI(2)); int t = read(); while (t--) { solve(); } return 0; } |