//Konrad Paluszek,University of Warsaw(former XIV LO Staszic) //it's nice to stick a fork into an open socket //clock says it's time to sleep //our getppid() in heaven, hallowed by ttyname #undef LOCAL2 #ifndef LOCAL #pragma GCC optimize("O3") #endif #define TIME (chrono::steady_clock::now().time_since_epoch().count()) #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define xfm(a,b)a##b #define xwb(a,b)xfm(a,b) #define _ xwb(nvj,__LINE__) #define FOR(i,b,e)for(urs((b)+(e))i=(b);i<=(e);++i) #define REP(i,n)for(urs(n)i=0;i<(n);++i) #define FORD(i,b,e)for(urs((b)+(e))i=(b);i>=(int)(e);--i) #define UNTIL(t)while(clock()<(t)*CLOCKS_PER_SEC) #define PER(r...)for(bool _=1;_||next_permutation(r);_=false) #define ALL(r)(r).begin(),(r).end() #define FS(r)r.first,r.second #define sim template<class c #define ros return o #define rans return ans #define forbits(i,m)if(m)for(urs(m)i=ctz(m),i##nvj=m;i##nvj;i##nvj^=((urs(m))1<<i),i=ctz(i##nvj)) #define fordbits(i,m)if(m)for(urs(m)i=8*sizeof(m)-clz(m)-1,i##nxd=m;i##nxd;i##nxd^=((urs(m))1<<i),i=8*sizeof(m)-clz(i##nxd)-1) #define ksets(t, m, k, n) for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m)) #define urs(r...)typename decay<decltype(r)>::type #define hur(f,g,r)sim>int f(c a){if(sizeof(c)==16)return r;if(sizeof(c)==8)return g##ll(a);return g(a);} #define pwq(t,i) int clz(t x){return clz<int>(x)-i;} using namespace __gnu_pbds;using namespace std;using ll=long long;using ld = long double;using ull=unsigned long long;using pii=pair<int,int>;using pll=pair<ll,ll>;using vpii=vector<pii>;using unt=unsigned int;sim>using min_queue=priority_queue<c,vector<c>,greater<c>>;sim>using ordered_set=tree<c,null_type,less<c>,rb_tree_tag,tree_order_statistics_node_update>;hur(popc,__builtin_popcount,popc<ull>(a)+popc<ull>(a>>64))hur(ctz,__builtin_ctz,(ull)a?ctz<ull>(a):64+ctz<ull>(a>>64))hur(clz,__builtin_clz,a>>64?clz<ull>(a>>64):64+clz<ull>(a))pwq(short,16)pwq(uint16_t,16)pwq(char,24)pwq(int8_t,24)pwq(uint8_t,24)sim,class N>bool mini(c&o,const N&h){if(o>h)ros=h,1;return 0;}sim,class N>bool maxi(c&o,const N&h){if(o<h)ros=h,1;return 0;}sim,class n>using gyv=c; #define oor >ostream&operator<<(ostream&o, #ifdef LOCAL #define loc(r...)r #define onl(r...) #define azm(r)sim,class n,class l,class...v>bool emz1(const r<c,n,v...>&o,l b){ros.count(b);}sim,class n,class l,class...v>const n&emz2(const r<c,n,v...>&o,l b){ros.find(b)->second;} #define debug(c...)::dor(1,vqx(__LINE__,__FUNCTION__,__PRETTY_FUNCTION__),c) #define rcu m);}g(+)g(-)g(*)g(/)g(%)g(<<)g(>>)g(^)g(|)g(&)g(>=)g(<=)g(>)g(<)g(!=)g(==) #define zub(h,t,n...)class N>auto operator h(n u)->nep<urs(t)>{return ngy(t, #define g(o)template<zub(o,v o u,N)tfj(bte(*this))+" " #o " "+tfj(bte(u)),m);} sim>gyv<int,typename c::value_type>dud(int x);sim>char dud(...);struct kav;sim>struct lfu;sim> struct jat;struct kav{static const int d=0;};sim>struct lfu{static const int d=jat<typename c::value_type>::d + 1;};sim>struct jat{static const int d=conditional<sizeof(dud<c>(0))==1,kav,lfu<c>>::type::d;};sim,class n>struct jat<pair<c,n>>{static const int d=jat<c>::d>jat<n>::d?jat<c>::d:jat<n>::d;};template<>struct jat<string>{static const int d=0;}; vector <pair <string, string> > repl = {{"std::", ""}, {"__debug::", ""}, {"long long unsigned int", "ull"}, {"long unsigned int", "size_t"}, {"unsigned int", "unt"}, {"long long int", "ll"}, {"vector<pair<int, int> >", "vpii"}, {"pair<int, int>", "pii"}, {"pair<long double, long double>", "pdd"}, {"long double", "ld"}}; string xif(string r) {string o;int d=0;for(char c:r) if(c=='[')d++;else if(c==']')d--;else if(d)o+=c;for(auto p:repl)while(true){unt f=o.find(p.first);if(f<o.size())o.replace(f,p.first.size(),p.second);else break;}if(o.size())o=" "+o;ros;} sim>c&lum(c&o){ros;}int lum(int8_t o){ros;}unt lum(uint8_t o){ros;}int uuv=6;void set_pre(int x){uuv=x;}sim>struct yda{c x;ll p,q,r,s;};sim>yda<typename std::decay<c>::type>nyb(c x,ll p,ll q,ll r,ll s){return{x,p,q,r,s};}azm(map)azm(unordered_map)sim>bool emz1(const c&a,ll o){ros>=0&&o<(ll)a.size();}sim>auto emz2(const c&o,ll x)->decltype(o[x]){ros[x];}sim>bool emz1(c*a,ll o){ros>=0;}sim>c&emz2(c*o,ll b){ros[b];}template<size_t n>bool emz1(const bitset<n>&,ll o){ros>=0&&o<(ll)n;}template<size_t n>int emz2(const bitset<n>&o,ll b){ros[b];}string tfj(string o){for(char x:o)if(x=='=')return"("+o+")";ros;}string vqx(int l,string f,string pf){stringstream o;o<<"\033[1;"<<30+l%8<<"m"<<f<<xif(pf)<<"#"<<l<<": \033["<<30+l%7+(l%7>=l%8)<<"m";ros.str();}ostream&operator<<(ostream&o,__int128 x){int D[50],c=0;if(x<0)o<<"-",x=-x;do{D[c++]=x%10;x/=10;}while(x);while(c)o<<D[--c];ros;}ostream&operator<<(ostream&o,__uint128_t x){int D[50],c=0;do{D[c++]=x%10;x/=10;}while(x);while(c)o<<D[--c];ros;}ostream&operator<<(ostream&o,__float128 x){ros<<(ld)x;}sim,class N oor pair<c,N>u){ros<<"<"<<lum(u.first)<<", "<<lum(u.second)<<">";}sim,typename=typename enable_if<!is_same<c,string>::value,urs(c().end())>::type oor c u){int q=0;o<<"{";for(auto e:u)o<<", "+2*!q++<<lum(e);ros<<"}_"<<q;}void dor(bool x){cerr<<dec<<"\033[0m"<<endl;}sim,class...N>void dor(int w,c x,N...y){cerr<<setprecision(uuv)<<boolalpha<<x<<"; "+2*w;dor(0,y...);}sim>string bte(c u){stringstream o;o<<setprecision(uuv)<<boolalpha<<lum(u);ros.str();}sim>struct nep;sim>c yyo(nep<c>);sim>nep<c>ngy(c,string,int m=0);sim>c yyo(c o){ros;}sim>struct nep{c v;string d;int m;nep<c>mask(){return ngy(v,d,1);}template<zub([],v[yyo(u)],N)d+"["+bte(u)+"]",rcu};sim>nep<c>ngy(c v,string d,int m){return nep<c>{v,d,m};}sim>c yyo(nep<c>o){ros.v;}sim>int uhn(c){return 0;}sim>int uhn(nep<c>o){ros.m;} #undef g #define ckx(o)template<zub(o,o yyo(u),nep<N>)#o+tfj(bte(u)),u.m);} #define g(o)sim,zub(o,yyo(p)o u.v,c p,nep<N>)tfj(bte(p))+" " #o " "+tfj(bte(u)),uhn(p)+u.m);} #define ccy(r...)sim>class enable_if<r is_integral<c>::value,string>::type ema(c u){ #define qel(t)sim,class d,class...e oor t<c,d,e...>r){ros<<*(d*)&r;} qel(queue)qel(priority_queue)qel(stack)ccy(!)return bte(u);}ccy()stringstream o;o<<"[";int q=0;forbits(i,u)o<<", "+2*!q++<<lum(i);ros.str()+"]";}sim>string bte(nep<c>o){string v=(o.m?ema(o.v):bte(o.v));if(v==o.d)ros.d;ros.d+" = "+v;}sim oor nep<c>m){ros<<bte(rcu sim>nep<c>ngy(nep<c>o,string d="",int m=0){ros;}ckx(+)ckx(-)ckx(!)ckx(~)sim oor yda<c>M){if(M.p>=M.q||M.r>=M.s)ros<<"[------]";int l=0;stringstream p;vector<vector<string>>X;vector<int>Y(M.s-M.r,1);FOR(i,M.p,M.q-1){maxi(l,(int)bte(i).size());X.emplace_back();if(emz1(M.x,i)){auto V=emz2(M.x,i);FOR(j,M.r,M.s-1){if(emz1(V,j)){auto Q=emz2(V,j);X.back().push_back(bte(Q));maxi(Y[j-M.r],(int)X.back().back().size());}else X.back().push_back("3Ju+");}}}FOR(i,M.p,M.q-1){p<<"\n"<<setw(l)<<i<<": ";if(X[i-M.p].size())FOR(j,M.r,M.s-1){if(X[i-M.p][j-M.r]=="3Ju+")REP(_,Y[j-M.r])p<<"-";else p<<setw(Y[j-M.r])<<X[i-M.p][j-M.r];p<<"; ";}}p<<"\n";ros<<p.str();}vector<string>fdn;struct bcx{bcx(){fdn.push_back("");}~bcx(){fdn.pop_back();}};void imx(string m){cerr<<m<<"STACK TRACE: "<<dec<<"\033[0m"<<endl;for(auto x:fdn)cerr<<"\t"<<x<<"\033[0m"<<endl;}void ctm(bool r){fdn.back()+="\033[m";}sim,class...n>void ctm(bool r,c x,n...y){fdn.back()+=bte(x)+("; "+2*r);ctm(0,y...);} #undef g #define imie(a...) ngy(a,#a) #define imask(a...) ngy(a,#a,1) #define ra3(u,g,c) ngy(list<urs((u)[0])>(begin(u)+yyo(g),begin(u)+yyo(c)),#u "["+bte(g)+"..."+bte(c)+")") #define ra2(i,j)ngy(list<urs(*(i))>(i,j),"[" #i "..." #j ")") #define zoz(a,b,c,d,e...) d #define range(r...) zoz(r,ra3,ra2)(r) #define arr(a, i) imie(a)[imie(i)] #define arr2(a, i, j) imie(a)[imie(i)][imie(j)] #define print_stack(r...)::imx(vqx(__LINE__,__FUNCTION__,__PRETTY_FUNCTION__)) #define mark_stack(r...)bcx _;::ctm(1,vqx(__LINE__,__FUNCTION__,__PRETTY_FUNCTION__),r) #define dump(a,i,j,k,q)ngy(nyb(a,yyo(i),yyo(j),yyo(k),yyo(q)),#a "["+bte(imie(i))+".."+bte(imie(j))+")["+bte(imie(k))+".."+bte(imie(q))+")") #else #define loc(...) #define onl(r...)r #define debug(...) #define print_stack(...) #define mark_stack(...) #define set_pre(...) #endif #define next nexT #define prev preV #define tree trEE #define left lefT #define right righT #define div diV #define y1 y_1 ull mix(ull o){o+=0x9e3779b97f4a7c15;o=(o^(o>>30))*0xbf58476d1ce4e5b9;o=(o^(o>>27))*0x94d049bb133111eb;ros^(o>>31);}ull SALT=0x7a14a4b0881ebf9,tqu=0x7a14a4b0881ebf9;ull my_rand(){return tqu=mix(tqu);}void my_srand(ull x){SALT=tqu=x;}const int inf = 1023400000;const ll llinf=1234567890000000000ll; #define rand my_rand #define srand my_srand #define random_shuffle(r...)random_shuffle(r,[](int _){return my_rand()%_;}) sim>inline c nux(c m){if(!m)return numeric_limits<c>::max();c A=m&-m;c B=~((A-1)^m);c C=B&-B;c D=(C>>(1+ctz(A)))-1;return C|(m&~(C-1))|D;}__attribute__((no_sanitize_undefined))ll mul(ll a,ll b,ll m){ll q=a*(ld)b/m;ll o=a*b-q*m;o%=m;if(o<0)o+=m;ros;}sim>void unq(c&x){x.resize(unique(ALL(x))-x.begin());}sim>c gcd(c o,c b){while(o&&b){int x=ctz(o),y=ctz(b);if(x>y)o>>=x-y;else b>>=y-x;if(o>b)o-=b;else b-=o;}ros?o:b;}sim>c lcm(c o,c b){ros/gcd(o,b)*b;} sim>typename enable_if<is_integral<c>::value,ull>::type my_hash(c x){ if(sizeof(c)>8) return mix((ull)x^SALT)^mix((ull)(x>>64)^SALT); return mix(x^SALT); } template<size_t N>ull my_hash(const bitset<N>&x){ return mix(hash<bitset<N>>()(x)^SALT); } sim,class n>ull my_hash(const pair<c,n>&); sim>gyv<ull,typename c::value_type>my_hash(const c&x){ ull o=SALT; for(auto&d:x)o=mix(o^my_hash(d)); ros; } sim,class n>ull my_hash(const pair<c,n>&x){return mix(my_hash(x.first))^my_hash(x.second);} sim>struct my_hasher{size_t operator()(const c&o)const{return my_hash(o);}}; sim,class m>using _un0rDer3d_M4P=loc(unordered_map)onl(cc_hash_table)<c,m,my_hasher<c>>; #define unordered_map _un0rDer3d_M4P string join(const vector<string>&x){stringstream ans;ans<<"\n";for(const string&c:x)ans<<c<<"\n";return ans.str();} sim,class F>auto get(F f,c x)->decltype(f[x]){return f[x];} sim,class F>auto get(F f,c x)->decltype(f(x)){return f(x);} vector<string>sp(string x){vector<string>ans(1);for(char c:x){if(c=='\n')ans.emplace_back();else ans.back().push_back(c);}rans;} sim,class F,class G>vector<string>draw_tree_rec(c t,const c*p,F s,G d){auto C=get(s,t);auto r=sp(d(t));int lr=0;for(auto&x:r)maxi(lr,(int)x.size());if(C.empty()||(p&&C==urs(C){*p})){for(auto&q:r)q.resize(lr,' ');return r;}vector<string>ans(0);int le=0;vector<int>vs;bool prev=false;for(c x:C){if(p&&x==*p)continue;auto curr=draw_tree_rec(x,&t,s,d);for(int i=ans.size();i<(int)curr.size();++i)ans.push_back(string(le,' '));if(prev){for(int i=0;i<(int)ans.size();++i)ans[i]+=' ';le++;}prev=true;for(int i=0;i<(int)curr.size();++i)ans[i]+=curr[i];for(int i=curr.size();i<(int)ans.size();++i)ans[i]+=string(curr[0].size(),' ');vs.push_back(le+curr[0].size()/2);le+=curr[0].size();}if(lr>le){int ladd=(lr-le)/2,radd=(lr-le+1)/2;for(int&x:vs)x+=ladd;for(int i=0;i<(int)ans.size();++i)ans[i]=string(ladd,' ')+ans[i]+string(radd,' ');le=lr;}int up_i=le/2;int pos=0;string bot;for(int i=0;i<le;++i){bool down=false,up=i==up_i,in=false;if(pos==(int)vs.size())in=i<up_i;else if(i==vs[pos]){in=down=true;pos++;}else if(pos==0)in=i>up_i;else if(pos==(int)vs.size())in=i<up_i;else in=true;if(up&&down)bot+='+';else if(up)bot+='^';else if(down)bot+='v';else if(in)bot+='-';else bot+=' ';}int ladd=(le-lr)/2;for(auto&q:r){q=string(ladd,' ')+q;q+=string(le-q.size(),' ');}r.push_back(bot);ans.insert(ans.begin(),ALL(r));for(int i=1;i<(int)ans.size();++i)assert(ans[i].size()==ans[0].size());rans;} sim,class F,class G>string draw_tree(c t,F s,G d){return join(draw_tree_rec(t,(c*)0,s,d));} sim,class F>string draw_tree(c t,F s){return draw_tree(t,s,[](c x){return bte(x);});} sim,class F,class G>string draw_tree(c t,c p,F s,G d){return join(draw_tree_rec(t,&p,s,d));} sim,class F>string draw_tree(c t,c p,F s){return draw_tree(t,p,s,[](c x){return bte(x);});} #define by_parents(p,n) [&](int xwb(vsu,__LINE__)){vector<int>o;for(int _=0;_<=(n);++_)if(p[_]==xwb(vsu,__LINE__)&&_!=xwb(vsu,__LINE__))o.push_back(_);ros;} sim> struct FIB{c x;}; sim> FIB<c> bifi(c x){return FIB<c>{x};} sim oor FIB<c> f) { c v = f.x; assert(v >= 0); o << v << " = "; vector <c> fibs = {1, 2}; while (*fibs.rbegin() + *(fibs.rbegin() + 1) <= v) fibs.push_back(*fibs.rbegin() + *(fibs.rbegin() + 1)); while (fibs.size() >= 2u && fibs.back() > v) fibs.pop_back(); FORD(i, (int)fibs.size() - 1, 0) { if (v >= fibs[i]) { o << 1; v -= fibs[i]; } else o << 0; } ros << "_" << fibs.size() - 1; } #define fibi(r) ngy(bifi(r), #r) #define XREP(i,n) for(int i = 0; i < int(n); ++i) /*Precision error max_ans/1e15 (2.5e18) for (long) doubles. So integer rounding works for doubles with answers 0.5*1e15, e.g. for sizes 2^20 and RANDOM positive integers up to 45k. Those values assume DBL_MANT_DIG=53 and LDBL_MANT_DIG=64. For input in [0, M], you can decrease everything by M/2. If there are many small vectors, uncomment "BRUTE FORCE".*/ typedef double dou; // 'long double' is 2.2 times slower struct C { dou real, imag; C operator * (const C & he) const { return C{real * he.real - imag * he.imag, real * he.imag + imag * he.real}; } void operator += (const C & he) { real += he.real; imag += he.imag; } }; void dft(vector<C> & a, bool rev) { const int n = a.size(); for(int i = 1, k = 0; i < n; ++i) { for(int bit = n / 2; (k ^= bit) < bit; bit /= 2);;; if(i < k) swap(a[i], a[k]); } for(int len = 1, who = 0; len < n; len *= 2, ++who) { static vector<C> t[30]; vector<C> & om = t[who]; if(om.empty()) { om.resize(len); const dou ang = 2 * acosl(0) / len; XREP(i, len) om[i] = i%2 || !who ? C{cos(i*ang), sin(i*ang)} : t[who-1][i/2]; } for(int i = 0; i < n; i += 2 * len) XREP(k, len) { const C x = a[i+k], y = a[i+k+len] * C{om[k].real, om[k].imag * (rev ? -1 : 1)}; a[i+k] += y; a[i+k+len] = C{x.real - y.real, x.imag - y.imag}; } } if(rev) XREP(i, n) a[i].real /= n; } template<typename T>vector<T> multiply(const vector<T> & a, const vector<T> & b, bool split = false) { if(a.empty() || b.empty()) return {}; int n = a.size() + b.size(); vector<T> ans(n - 1); /* if(min(a.size(),b.size()) < 190) { // BRUTE FORCE XREP(i, a.size()) XREP(j, b.size()) ans[i+j] += a[i]*b[j]; return ans; } */ while(n&(n-1)) ++n; // http://codeforces.com/blog/entry/48417 auto speed = [&](const vector<C> & w, int i, int k) { int j = i ? n - i : 0, r = k ? -1 : 1; return C{w[i].real + w[j].real * r, w[i].imag - w[j].imag * r} * (k ? C{0, -0.5} : C{0.5, 0}); }; if(!split) { // standard fast version vector<C> in(n), done(n); XREP(i, a.size()) in[i].real = a[i]; XREP(i, b.size()) in[i].imag = b[i]; dft(in, false); XREP(i, n) done[i] = speed(in, i, 0) * speed(in, i, 1); dft(done, true); XREP(i, ans.size()) ans[i] = is_integral<T>::value ? llround(done[i].real) : done[i].real; //XREP(i,ans.size())err=max(err,abs(done[i].real-ans[i])); } else { // Split big INTEGERS into pairs a1*M+a2, const T M = 150; // where M = sqrt(max_absvalue). vector<C> t[2]; // This version is 2.2-2.5 times slower. XREP(x, 2) { t[x].resize(n); auto & in = x ? b : a; XREP(i, in.size()) t[x][i]=C{dou(in[i]%M), dou(in[i]/M)}; dft(t[x], false); } T mul = 1; for(int s = 0; s < 3; ++s, mul *= M) { vector<C> prod(n); XREP(x, 2) XREP(y, 2) if(x + y == s) XREP(i, n) prod[i] += speed(t[0], i, x) * speed(t[1], i, y); dft(prod, true); XREP(i, ans.size()) ans[i]+= llround(prod[i].real)*mul; } } return ans; } sim> ll calc_mod(const vector <c> &d, ll mod) { ll a = 1, b = 1; ll val = 0; for (c x : d) { if (x) val = (val + mul(x, b, mod)) % mod; ll v = (a + b) % mod; a = b; b = v; } return val; } sim> void assert_good(const vector <c> &d) { REP(i, (int) d.size() - 1) assert(d[i] * d[i + 1] == 0); for (int x : d) assert(x == 0 || x == 1); assert(!d.empty() && d.back() == 1); } sim> void fix2(vector <c> &f) { loc (ll mod = rand() % (ll) 1e18 + 1; ll v = calc_mod(f, mod); ) f.resize(f.size() + 80); int n = f.size(); bool any = true; loc(int turns = 0); while (any) { loc(turns++); any = false; REP(i, n - 2) { c push = min(f[i], f[i + 1]); if (push) any = true; f[i] -= push; f[i + 1] -= push; f[i + 2] += push; } REP(i, n - 2) { c push = f[i] / 2; if (push) any = true; if (i == 0) { f[i] -= 2 * push; f[i + 1] += push; } else if (i == 1) { f[i - 1] += push; f[i] -= 2 * push; f[i + 1] += push; } else { f[i - 2] += push; f[i] -= 2 * push; f[i + 1] += push; } } } while (f.back() == 0) f.pop_back(); debug(imie(turns)); loc(ll v2 = calc_mod(f, mod); assert(v == v2); assert_good(f); ) } vector <ll> fib_mul(const vector <int> &a, const vector <int> &b) { debug("start"); vector <ll> ans(a.size() + b.size() + 40); loc(ll mod = rand() % (ll) 1e18 + 1;) vector <int> m1 = multiply(a, b); #define NEXT #ifndef NEXT for (int x = (int) m1.size() - 1; x >= 0; --x) ans[x] = m1[x] + ans[x + 4]; ans[0] += ans[3]; #endif ll at_neg1 = 0; vector <int> as[2], bs[2]; vector <ll> ans2(ans.size()); REP(i, a.size()) as[i & 1].push_back(a[i]); REP(i, b.size()) bs[i & 1].push_back(b[i]); reverse(ALL(bs[0])); reverse(ALL(bs[1])); // debug(imie(a), imie(b)); REP(ap, 2) REP(bp, 2) { // debug(imie(ap), imie(bp)); vector <int> m = multiply(as[ap], bs[bp]); // debug(arr(as, ap), arr(bs, bp), imie(m)); REP(i, (int)m.size()) { int diff = 2 * (i - bs[bp].size() + 1) + ap - bp; int low = diff < 0 ? ap : bp; int s2 = abs(diff) + 2 * low - 4; // int should = 0; // REP(aa, (int)a.size()) REP(bb, (int)b.size()) { // if (aa - bb == diff && low == (min(aa, bb) & 1)) { // debug(arr(a, aa) * arr(b, bb)); // should += a[aa] * b[bb]; // } // } // debug(imie(diff), imie(low), imie(s2), arr(m, i), imie(should)); // assert(m[i] == should); #ifdef NEXT //debug(imie(s2), arr(m, i)); if (s2 >= -1) m1[max(0, s2)] -= m[i]; #else for (int x = s2; x >= -1; x -= 4) ans[max(0, x)] -= m[i]; #endif if (low) ans2[max(0, s2 + 1)] += m[i]; } } #ifdef NEXT for (int x = (int) m1.size() - 1; x >= 0; --x) ans[x] = m1[x] + ans[x + 4]; ans[0] += ans[3] + at_neg1; #endif REP(i, ans.size()) ans[i] += ans2[i]; // debug(imie(ans), " ?= {6, 6, 4, 4, 3, 2, 1}"); // REP(i, (int) a.size()) REP(j, (int) b.size()) { // if (a[i] && b[j]) { // int absdiff = abs(i - j); // int low = min(i, j) & 1; // int s2 = absdiff + 2 * low - 4; // for (int x = s2; x >= -1; x -= 4) // ans[max(0, x)]--; // if (low) // ans[max(0, s2 + 1)]++; // } // } // debug(imie(a), imie(b), imie(ans)); debug("start fix"); fix2(ans); debug("end fix"); #if defined(LOCAL) || defined(LOCAL2) ll amod = calc_mod(a, mod); ll bmod = calc_mod(b, mod); ll cmod = calc_mod(ans, mod); assert(mul(amod, bmod, mod) == cmod); #endif rans; } vector <int> get() { int l; scanf("%d", &l); vector <int> ans(l); REP(i, l) scanf("%d", &ans[i]); rans; } int main() { #warning Jak wzorcówka jest równie syfna jak mój rozw, to się pogniewamy xD int t; scanf("%d", &t); while (t--) { vector <int> a = get(), b = get(); vector <ll> c = fib_mul(a, b); printf("%d", (int) c.size()); for (ll x : c) printf(" %lld", x); printf("\n"); } }
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | //Konrad Paluszek,University of Warsaw(former XIV LO Staszic) //it's nice to stick a fork into an open socket //clock says it's time to sleep //our getppid() in heaven, hallowed by ttyname #undef LOCAL2 #ifndef LOCAL #pragma GCC optimize("O3") #endif #define TIME (chrono::steady_clock::now().time_since_epoch().count()) #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define xfm(a,b)a##b #define xwb(a,b)xfm(a,b) #define _ xwb(nvj,__LINE__) #define FOR(i,b,e)for(urs((b)+(e))i=(b);i<=(e);++i) #define REP(i,n)for(urs(n)i=0;i<(n);++i) #define FORD(i,b,e)for(urs((b)+(e))i=(b);i>=(int)(e);--i) #define UNTIL(t)while(clock()<(t)*CLOCKS_PER_SEC) #define PER(r...)for(bool _=1;_||next_permutation(r);_=false) #define ALL(r)(r).begin(),(r).end() #define FS(r)r.first,r.second #define sim template<class c #define ros return o #define rans return ans #define forbits(i,m)if(m)for(urs(m)i=ctz(m),i##nvj=m;i##nvj;i##nvj^=((urs(m))1<<i),i=ctz(i##nvj)) #define fordbits(i,m)if(m)for(urs(m)i=8*sizeof(m)-clz(m)-1,i##nxd=m;i##nxd;i##nxd^=((urs(m))1<<i),i=8*sizeof(m)-clz(i##nxd)-1) #define ksets(t, m, k, n) for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m)) #define urs(r...)typename decay<decltype(r)>::type #define hur(f,g,r)sim>int f(c a){if(sizeof(c)==16)return r;if(sizeof(c)==8)return g##ll(a);return g(a);} #define pwq(t,i) int clz(t x){return clz<int>(x)-i;} using namespace __gnu_pbds;using namespace std;using ll=long long;using ld = long double;using ull=unsigned long long;using pii=pair<int,int>;using pll=pair<ll,ll>;using vpii=vector<pii>;using unt=unsigned int;sim>using min_queue=priority_queue<c,vector<c>,greater<c>>;sim>using ordered_set=tree<c,null_type,less<c>,rb_tree_tag,tree_order_statistics_node_update>;hur(popc,__builtin_popcount,popc<ull>(a)+popc<ull>(a>>64))hur(ctz,__builtin_ctz,(ull)a?ctz<ull>(a):64+ctz<ull>(a>>64))hur(clz,__builtin_clz,a>>64?clz<ull>(a>>64):64+clz<ull>(a))pwq(short,16)pwq(uint16_t,16)pwq(char,24)pwq(int8_t,24)pwq(uint8_t,24)sim,class N>bool mini(c&o,const N&h){if(o>h)ros=h,1;return 0;}sim,class N>bool maxi(c&o,const N&h){if(o<h)ros=h,1;return 0;}sim,class n>using gyv=c; #define oor >ostream&operator<<(ostream&o, #ifdef LOCAL #define loc(r...)r #define onl(r...) #define azm(r)sim,class n,class l,class...v>bool emz1(const r<c,n,v...>&o,l b){ros.count(b);}sim,class n,class l,class...v>const n&emz2(const r<c,n,v...>&o,l b){ros.find(b)->second;} #define debug(c...)::dor(1,vqx(__LINE__,__FUNCTION__,__PRETTY_FUNCTION__),c) #define rcu m);}g(+)g(-)g(*)g(/)g(%)g(<<)g(>>)g(^)g(|)g(&)g(>=)g(<=)g(>)g(<)g(!=)g(==) #define zub(h,t,n...)class N>auto operator h(n u)->nep<urs(t)>{return ngy(t, #define g(o)template<zub(o,v o u,N)tfj(bte(*this))+" " #o " "+tfj(bte(u)),m);} sim>gyv<int,typename c::value_type>dud(int x);sim>char dud(...);struct kav;sim>struct lfu;sim> struct jat;struct kav{static const int d=0;};sim>struct lfu{static const int d=jat<typename c::value_type>::d + 1;};sim>struct jat{static const int d=conditional<sizeof(dud<c>(0))==1,kav,lfu<c>>::type::d;};sim,class n>struct jat<pair<c,n>>{static const int d=jat<c>::d>jat<n>::d?jat<c>::d:jat<n>::d;};template<>struct jat<string>{static const int d=0;}; vector <pair <string, string> > repl = {{"std::", ""}, {"__debug::", ""}, {"long long unsigned int", "ull"}, {"long unsigned int", "size_t"}, {"unsigned int", "unt"}, {"long long int", "ll"}, {"vector<pair<int, int> >", "vpii"}, {"pair<int, int>", "pii"}, {"pair<long double, long double>", "pdd"}, {"long double", "ld"}}; string xif(string r) {string o;int d=0;for(char c:r) if(c=='[')d++;else if(c==']')d--;else if(d)o+=c;for(auto p:repl)while(true){unt f=o.find(p.first);if(f<o.size())o.replace(f,p.first.size(),p.second);else break;}if(o.size())o=" "+o;ros;} sim>c&lum(c&o){ros;}int lum(int8_t o){ros;}unt lum(uint8_t o){ros;}int uuv=6;void set_pre(int x){uuv=x;}sim>struct yda{c x;ll p,q,r,s;};sim>yda<typename std::decay<c>::type>nyb(c x,ll p,ll q,ll r,ll s){return{x,p,q,r,s};}azm(map)azm(unordered_map)sim>bool emz1(const c&a,ll o){ros>=0&&o<(ll)a.size();}sim>auto emz2(const c&o,ll x)->decltype(o[x]){ros[x];}sim>bool emz1(c*a,ll o){ros>=0;}sim>c&emz2(c*o,ll b){ros[b];}template<size_t n>bool emz1(const bitset<n>&,ll o){ros>=0&&o<(ll)n;}template<size_t n>int emz2(const bitset<n>&o,ll b){ros[b];}string tfj(string o){for(char x:o)if(x=='=')return"("+o+")";ros;}string vqx(int l,string f,string pf){stringstream o;o<<"\033[1;"<<30+l%8<<"m"<<f<<xif(pf)<<"#"<<l<<": \033["<<30+l%7+(l%7>=l%8)<<"m";ros.str();}ostream&operator<<(ostream&o,__int128 x){int D[50],c=0;if(x<0)o<<"-",x=-x;do{D[c++]=x%10;x/=10;}while(x);while(c)o<<D[--c];ros;}ostream&operator<<(ostream&o,__uint128_t x){int D[50],c=0;do{D[c++]=x%10;x/=10;}while(x);while(c)o<<D[--c];ros;}ostream&operator<<(ostream&o,__float128 x){ros<<(ld)x;}sim,class N oor pair<c,N>u){ros<<"<"<<lum(u.first)<<", "<<lum(u.second)<<">";}sim,typename=typename enable_if<!is_same<c,string>::value,urs(c().end())>::type oor c u){int q=0;o<<"{";for(auto e:u)o<<", "+2*!q++<<lum(e);ros<<"}_"<<q;}void dor(bool x){cerr<<dec<<"\033[0m"<<endl;}sim,class...N>void dor(int w,c x,N...y){cerr<<setprecision(uuv)<<boolalpha<<x<<"; "+2*w;dor(0,y...);}sim>string bte(c u){stringstream o;o<<setprecision(uuv)<<boolalpha<<lum(u);ros.str();}sim>struct nep;sim>c yyo(nep<c>);sim>nep<c>ngy(c,string,int m=0);sim>c yyo(c o){ros;}sim>struct nep{c v;string d;int m;nep<c>mask(){return ngy(v,d,1);}template<zub([],v[yyo(u)],N)d+"["+bte(u)+"]",rcu};sim>nep<c>ngy(c v,string d,int m){return nep<c>{v,d,m};}sim>c yyo(nep<c>o){ros.v;}sim>int uhn(c){return 0;}sim>int uhn(nep<c>o){ros.m;} #undef g #define ckx(o)template<zub(o,o yyo(u),nep<N>)#o+tfj(bte(u)),u.m);} #define g(o)sim,zub(o,yyo(p)o u.v,c p,nep<N>)tfj(bte(p))+" " #o " "+tfj(bte(u)),uhn(p)+u.m);} #define ccy(r...)sim>class enable_if<r is_integral<c>::value,string>::type ema(c u){ #define qel(t)sim,class d,class...e oor t<c,d,e...>r){ros<<*(d*)&r;} qel(queue)qel(priority_queue)qel(stack)ccy(!)return bte(u);}ccy()stringstream o;o<<"[";int q=0;forbits(i,u)o<<", "+2*!q++<<lum(i);ros.str()+"]";}sim>string bte(nep<c>o){string v=(o.m?ema(o.v):bte(o.v));if(v==o.d)ros.d;ros.d+" = "+v;}sim oor nep<c>m){ros<<bte(rcu sim>nep<c>ngy(nep<c>o,string d="",int m=0){ros;}ckx(+)ckx(-)ckx(!)ckx(~)sim oor yda<c>M){if(M.p>=M.q||M.r>=M.s)ros<<"[------]";int l=0;stringstream p;vector<vector<string>>X;vector<int>Y(M.s-M.r,1);FOR(i,M.p,M.q-1){maxi(l,(int)bte(i).size());X.emplace_back();if(emz1(M.x,i)){auto V=emz2(M.x,i);FOR(j,M.r,M.s-1){if(emz1(V,j)){auto Q=emz2(V,j);X.back().push_back(bte(Q));maxi(Y[j-M.r],(int)X.back().back().size());}else X.back().push_back("3Ju+");}}}FOR(i,M.p,M.q-1){p<<"\n"<<setw(l)<<i<<": ";if(X[i-M.p].size())FOR(j,M.r,M.s-1){if(X[i-M.p][j-M.r]=="3Ju+")REP(_,Y[j-M.r])p<<"-";else p<<setw(Y[j-M.r])<<X[i-M.p][j-M.r];p<<"; ";}}p<<"\n";ros<<p.str();}vector<string>fdn;struct bcx{bcx(){fdn.push_back("");}~bcx(){fdn.pop_back();}};void imx(string m){cerr<<m<<"STACK TRACE: "<<dec<<"\033[0m"<<endl;for(auto x:fdn)cerr<<"\t"<<x<<"\033[0m"<<endl;}void ctm(bool r){fdn.back()+="\033[m";}sim,class...n>void ctm(bool r,c x,n...y){fdn.back()+=bte(x)+("; "+2*r);ctm(0,y...);} #undef g #define imie(a...) ngy(a,#a) #define imask(a...) ngy(a,#a,1) #define ra3(u,g,c) ngy(list<urs((u)[0])>(begin(u)+yyo(g),begin(u)+yyo(c)),#u "["+bte(g)+"..."+bte(c)+")") #define ra2(i,j)ngy(list<urs(*(i))>(i,j),"[" #i "..." #j ")") #define zoz(a,b,c,d,e...) d #define range(r...) zoz(r,ra3,ra2)(r) #define arr(a, i) imie(a)[imie(i)] #define arr2(a, i, j) imie(a)[imie(i)][imie(j)] #define print_stack(r...)::imx(vqx(__LINE__,__FUNCTION__,__PRETTY_FUNCTION__)) #define mark_stack(r...)bcx _;::ctm(1,vqx(__LINE__,__FUNCTION__,__PRETTY_FUNCTION__),r) #define dump(a,i,j,k,q)ngy(nyb(a,yyo(i),yyo(j),yyo(k),yyo(q)),#a "["+bte(imie(i))+".."+bte(imie(j))+")["+bte(imie(k))+".."+bte(imie(q))+")") #else #define loc(...) #define onl(r...)r #define debug(...) #define print_stack(...) #define mark_stack(...) #define set_pre(...) #endif #define next nexT #define prev preV #define tree trEE #define left lefT #define right righT #define div diV #define y1 y_1 ull mix(ull o){o+=0x9e3779b97f4a7c15;o=(o^(o>>30))*0xbf58476d1ce4e5b9;o=(o^(o>>27))*0x94d049bb133111eb;ros^(o>>31);}ull SALT=0x7a14a4b0881ebf9,tqu=0x7a14a4b0881ebf9;ull my_rand(){return tqu=mix(tqu);}void my_srand(ull x){SALT=tqu=x;}const int inf = 1023400000;const ll llinf=1234567890000000000ll; #define rand my_rand #define srand my_srand #define random_shuffle(r...)random_shuffle(r,[](int _){return my_rand()%_;}) sim>inline c nux(c m){if(!m)return numeric_limits<c>::max();c A=m&-m;c B=~((A-1)^m);c C=B&-B;c D=(C>>(1+ctz(A)))-1;return C|(m&~(C-1))|D;}__attribute__((no_sanitize_undefined))ll mul(ll a,ll b,ll m){ll q=a*(ld)b/m;ll o=a*b-q*m;o%=m;if(o<0)o+=m;ros;}sim>void unq(c&x){x.resize(unique(ALL(x))-x.begin());}sim>c gcd(c o,c b){while(o&&b){int x=ctz(o),y=ctz(b);if(x>y)o>>=x-y;else b>>=y-x;if(o>b)o-=b;else b-=o;}ros?o:b;}sim>c lcm(c o,c b){ros/gcd(o,b)*b;} sim>typename enable_if<is_integral<c>::value,ull>::type my_hash(c x){ if(sizeof(c)>8) return mix((ull)x^SALT)^mix((ull)(x>>64)^SALT); return mix(x^SALT); } template<size_t N>ull my_hash(const bitset<N>&x){ return mix(hash<bitset<N>>()(x)^SALT); } sim,class n>ull my_hash(const pair<c,n>&); sim>gyv<ull,typename c::value_type>my_hash(const c&x){ ull o=SALT; for(auto&d:x)o=mix(o^my_hash(d)); ros; } sim,class n>ull my_hash(const pair<c,n>&x){return mix(my_hash(x.first))^my_hash(x.second);} sim>struct my_hasher{size_t operator()(const c&o)const{return my_hash(o);}}; sim,class m>using _un0rDer3d_M4P=loc(unordered_map)onl(cc_hash_table)<c,m,my_hasher<c>>; #define unordered_map _un0rDer3d_M4P string join(const vector<string>&x){stringstream ans;ans<<"\n";for(const string&c:x)ans<<c<<"\n";return ans.str();} sim,class F>auto get(F f,c x)->decltype(f[x]){return f[x];} sim,class F>auto get(F f,c x)->decltype(f(x)){return f(x);} vector<string>sp(string x){vector<string>ans(1);for(char c:x){if(c=='\n')ans.emplace_back();else ans.back().push_back(c);}rans;} sim,class F,class G>vector<string>draw_tree_rec(c t,const c*p,F s,G d){auto C=get(s,t);auto r=sp(d(t));int lr=0;for(auto&x:r)maxi(lr,(int)x.size());if(C.empty()||(p&&C==urs(C){*p})){for(auto&q:r)q.resize(lr,' ');return r;}vector<string>ans(0);int le=0;vector<int>vs;bool prev=false;for(c x:C){if(p&&x==*p)continue;auto curr=draw_tree_rec(x,&t,s,d);for(int i=ans.size();i<(int)curr.size();++i)ans.push_back(string(le,' '));if(prev){for(int i=0;i<(int)ans.size();++i)ans[i]+=' ';le++;}prev=true;for(int i=0;i<(int)curr.size();++i)ans[i]+=curr[i];for(int i=curr.size();i<(int)ans.size();++i)ans[i]+=string(curr[0].size(),' ');vs.push_back(le+curr[0].size()/2);le+=curr[0].size();}if(lr>le){int ladd=(lr-le)/2,radd=(lr-le+1)/2;for(int&x:vs)x+=ladd;for(int i=0;i<(int)ans.size();++i)ans[i]=string(ladd,' ')+ans[i]+string(radd,' ');le=lr;}int up_i=le/2;int pos=0;string bot;for(int i=0;i<le;++i){bool down=false,up=i==up_i,in=false;if(pos==(int)vs.size())in=i<up_i;else if(i==vs[pos]){in=down=true;pos++;}else if(pos==0)in=i>up_i;else if(pos==(int)vs.size())in=i<up_i;else in=true;if(up&&down)bot+='+';else if(up)bot+='^';else if(down)bot+='v';else if(in)bot+='-';else bot+=' ';}int ladd=(le-lr)/2;for(auto&q:r){q=string(ladd,' ')+q;q+=string(le-q.size(),' ');}r.push_back(bot);ans.insert(ans.begin(),ALL(r));for(int i=1;i<(int)ans.size();++i)assert(ans[i].size()==ans[0].size());rans;} sim,class F,class G>string draw_tree(c t,F s,G d){return join(draw_tree_rec(t,(c*)0,s,d));} sim,class F>string draw_tree(c t,F s){return draw_tree(t,s,[](c x){return bte(x);});} sim,class F,class G>string draw_tree(c t,c p,F s,G d){return join(draw_tree_rec(t,&p,s,d));} sim,class F>string draw_tree(c t,c p,F s){return draw_tree(t,p,s,[](c x){return bte(x);});} #define by_parents(p,n) [&](int xwb(vsu,__LINE__)){vector<int>o;for(int _=0;_<=(n);++_)if(p[_]==xwb(vsu,__LINE__)&&_!=xwb(vsu,__LINE__))o.push_back(_);ros;} sim> struct FIB{c x;}; sim> FIB<c> bifi(c x){return FIB<c>{x};} sim oor FIB<c> f) { c v = f.x; assert(v >= 0); o << v << " = "; vector <c> fibs = {1, 2}; while (*fibs.rbegin() + *(fibs.rbegin() + 1) <= v) fibs.push_back(*fibs.rbegin() + *(fibs.rbegin() + 1)); while (fibs.size() >= 2u && fibs.back() > v) fibs.pop_back(); FORD(i, (int)fibs.size() - 1, 0) { if (v >= fibs[i]) { o << 1; v -= fibs[i]; } else o << 0; } ros << "_" << fibs.size() - 1; } #define fibi(r) ngy(bifi(r), #r) #define XREP(i,n) for(int i = 0; i < int(n); ++i) /*Precision error max_ans/1e15 (2.5e18) for (long) doubles. So integer rounding works for doubles with answers 0.5*1e15, e.g. for sizes 2^20 and RANDOM positive integers up to 45k. Those values assume DBL_MANT_DIG=53 and LDBL_MANT_DIG=64. For input in [0, M], you can decrease everything by M/2. If there are many small vectors, uncomment "BRUTE FORCE".*/ typedef double dou; // 'long double' is 2.2 times slower struct C { dou real, imag; C operator * (const C & he) const { return C{real * he.real - imag * he.imag, real * he.imag + imag * he.real}; } void operator += (const C & he) { real += he.real; imag += he.imag; } }; void dft(vector<C> & a, bool rev) { const int n = a.size(); for(int i = 1, k = 0; i < n; ++i) { for(int bit = n / 2; (k ^= bit) < bit; bit /= 2);;; if(i < k) swap(a[i], a[k]); } for(int len = 1, who = 0; len < n; len *= 2, ++who) { static vector<C> t[30]; vector<C> & om = t[who]; if(om.empty()) { om.resize(len); const dou ang = 2 * acosl(0) / len; XREP(i, len) om[i] = i%2 || !who ? C{cos(i*ang), sin(i*ang)} : t[who-1][i/2]; } for(int i = 0; i < n; i += 2 * len) XREP(k, len) { const C x = a[i+k], y = a[i+k+len] * C{om[k].real, om[k].imag * (rev ? -1 : 1)}; a[i+k] += y; a[i+k+len] = C{x.real - y.real, x.imag - y.imag}; } } if(rev) XREP(i, n) a[i].real /= n; } template<typename T>vector<T> multiply(const vector<T> & a, const vector<T> & b, bool split = false) { if(a.empty() || b.empty()) return {}; int n = a.size() + b.size(); vector<T> ans(n - 1); /* if(min(a.size(),b.size()) < 190) { // BRUTE FORCE XREP(i, a.size()) XREP(j, b.size()) ans[i+j] += a[i]*b[j]; return ans; } */ while(n&(n-1)) ++n; // http://codeforces.com/blog/entry/48417 auto speed = [&](const vector<C> & w, int i, int k) { int j = i ? n - i : 0, r = k ? -1 : 1; return C{w[i].real + w[j].real * r, w[i].imag - w[j].imag * r} * (k ? C{0, -0.5} : C{0.5, 0}); }; if(!split) { // standard fast version vector<C> in(n), done(n); XREP(i, a.size()) in[i].real = a[i]; XREP(i, b.size()) in[i].imag = b[i]; dft(in, false); XREP(i, n) done[i] = speed(in, i, 0) * speed(in, i, 1); dft(done, true); XREP(i, ans.size()) ans[i] = is_integral<T>::value ? llround(done[i].real) : done[i].real; //XREP(i,ans.size())err=max(err,abs(done[i].real-ans[i])); } else { // Split big INTEGERS into pairs a1*M+a2, const T M = 150; // where M = sqrt(max_absvalue). vector<C> t[2]; // This version is 2.2-2.5 times slower. XREP(x, 2) { t[x].resize(n); auto & in = x ? b : a; XREP(i, in.size()) t[x][i]=C{dou(in[i]%M), dou(in[i]/M)}; dft(t[x], false); } T mul = 1; for(int s = 0; s < 3; ++s, mul *= M) { vector<C> prod(n); XREP(x, 2) XREP(y, 2) if(x + y == s) XREP(i, n) prod[i] += speed(t[0], i, x) * speed(t[1], i, y); dft(prod, true); XREP(i, ans.size()) ans[i]+= llround(prod[i].real)*mul; } } return ans; } sim> ll calc_mod(const vector <c> &d, ll mod) { ll a = 1, b = 1; ll val = 0; for (c x : d) { if (x) val = (val + mul(x, b, mod)) % mod; ll v = (a + b) % mod; a = b; b = v; } return val; } sim> void assert_good(const vector <c> &d) { REP(i, (int) d.size() - 1) assert(d[i] * d[i + 1] == 0); for (int x : d) assert(x == 0 || x == 1); assert(!d.empty() && d.back() == 1); } sim> void fix2(vector <c> &f) { loc (ll mod = rand() % (ll) 1e18 + 1; ll v = calc_mod(f, mod); ) f.resize(f.size() + 80); int n = f.size(); bool any = true; loc(int turns = 0); while (any) { loc(turns++); any = false; REP(i, n - 2) { c push = min(f[i], f[i + 1]); if (push) any = true; f[i] -= push; f[i + 1] -= push; f[i + 2] += push; } REP(i, n - 2) { c push = f[i] / 2; if (push) any = true; if (i == 0) { f[i] -= 2 * push; f[i + 1] += push; } else if (i == 1) { f[i - 1] += push; f[i] -= 2 * push; f[i + 1] += push; } else { f[i - 2] += push; f[i] -= 2 * push; f[i + 1] += push; } } } while (f.back() == 0) f.pop_back(); debug(imie(turns)); loc(ll v2 = calc_mod(f, mod); assert(v == v2); assert_good(f); ) } vector <ll> fib_mul(const vector <int> &a, const vector <int> &b) { debug("start"); vector <ll> ans(a.size() + b.size() + 40); loc(ll mod = rand() % (ll) 1e18 + 1;) vector <int> m1 = multiply(a, b); #define NEXT #ifndef NEXT for (int x = (int) m1.size() - 1; x >= 0; --x) ans[x] = m1[x] + ans[x + 4]; ans[0] += ans[3]; #endif ll at_neg1 = 0; vector <int> as[2], bs[2]; vector <ll> ans2(ans.size()); REP(i, a.size()) as[i & 1].push_back(a[i]); REP(i, b.size()) bs[i & 1].push_back(b[i]); reverse(ALL(bs[0])); reverse(ALL(bs[1])); // debug(imie(a), imie(b)); REP(ap, 2) REP(bp, 2) { // debug(imie(ap), imie(bp)); vector <int> m = multiply(as[ap], bs[bp]); // debug(arr(as, ap), arr(bs, bp), imie(m)); REP(i, (int)m.size()) { int diff = 2 * (i - bs[bp].size() + 1) + ap - bp; int low = diff < 0 ? ap : bp; int s2 = abs(diff) + 2 * low - 4; // int should = 0; // REP(aa, (int)a.size()) REP(bb, (int)b.size()) { // if (aa - bb == diff && low == (min(aa, bb) & 1)) { // debug(arr(a, aa) * arr(b, bb)); // should += a[aa] * b[bb]; // } // } // debug(imie(diff), imie(low), imie(s2), arr(m, i), imie(should)); // assert(m[i] == should); #ifdef NEXT //debug(imie(s2), arr(m, i)); if (s2 >= -1) m1[max(0, s2)] -= m[i]; #else for (int x = s2; x >= -1; x -= 4) ans[max(0, x)] -= m[i]; #endif if (low) ans2[max(0, s2 + 1)] += m[i]; } } #ifdef NEXT for (int x = (int) m1.size() - 1; x >= 0; --x) ans[x] = m1[x] + ans[x + 4]; ans[0] += ans[3] + at_neg1; #endif REP(i, ans.size()) ans[i] += ans2[i]; // debug(imie(ans), " ?= {6, 6, 4, 4, 3, 2, 1}"); // REP(i, (int) a.size()) REP(j, (int) b.size()) { // if (a[i] && b[j]) { // int absdiff = abs(i - j); // int low = min(i, j) & 1; // int s2 = absdiff + 2 * low - 4; // for (int x = s2; x >= -1; x -= 4) // ans[max(0, x)]--; // if (low) // ans[max(0, s2 + 1)]++; // } // } // debug(imie(a), imie(b), imie(ans)); debug("start fix"); fix2(ans); debug("end fix"); #if defined(LOCAL) || defined(LOCAL2) ll amod = calc_mod(a, mod); ll bmod = calc_mod(b, mod); ll cmod = calc_mod(ans, mod); assert(mul(amod, bmod, mod) == cmod); #endif rans; } vector <int> get() { int l; scanf("%d", &l); vector <int> ans(l); REP(i, l) scanf("%d", &ans[i]); rans; } int main() { #warning Jak wzorcówka jest równie syfna jak mój rozw, to się pogniewamy xD int t; scanf("%d", &t); while (t--) { vector <int> a = get(), b = get(); vector <ll> c = fib_mul(a, b); printf("%d", (int) c.size()); for (ll x : c) printf(" %lld", x); printf("\n"); } } |