#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; namespace { using Integer = long long; //__int128; template istream& operator >> (istream& is, vector& vec){for(T& val: vec) is >> val; return is;} template istream& operator , (istream& is, T& val){ return is >> val;} template ostream& operator << (ostream& os, const vector& vec){for(int i=0; i ostream& operator , (ostream& os, const T& val){ return os << " " << val;} template void print(const H& head){ cout << head; } template void print(const H& head, const T& ... tail){ cout << head << " "; print(tail...); } template void println(const T& ... values){ print(values...); cout << endl; } template void eprint(const H& head){ cerr << head; } template void eprint(const H& head, const T& ... tail){ cerr << head << " "; print(tail...); } template void eprintln(const T& ... values){ print(values...); cerr << endl; } string operator "" _s (const char* str, size_t size){ return move(string(str)); } constexpr Integer my_pow(Integer x, Integer k, Integer z=1){return k==0 ? z : k==1 ? z*x : (k&1) ? my_pow(x*x,k>>1,z*x) : my_pow(x*x,k>>1,z);} constexpr Integer my_pow_mod(Integer x, Integer k, Integer M, Integer z=1){return k==0 ? z%M : k==1 ? z*x%M : (k&1) ? my_pow_mod(x*x%M,k>>1,M,z*x%M) : my_pow_mod(x*x%M,k>>1,M,z);} constexpr unsigned long long operator "" _ten (unsigned long long value){ return my_pow(10,value); } inline int k_bit(Integer x, int k){return (x>>k)&1;} //0-indexed mt19937 mt(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()); template string join(const vector& v, const string& sep){ stringstream ss; for(int i=0; i0) ss << sep; ss << v[i]; } return ss.str(); } } constexpr long long mod = 9_ten + 7; #include template class Fast_Fourier_Transform{ // return the vector of F[t] or f[x] where // F[t] = sum of { f[x] * exp(-j * theta * t * x) } in x = 0 .. N-1 (FFT) // f(x) = 1/N * sum of { F[t] * exp(j * theta * t * x) } in t = 0 .. N-1 (inverse FFT) // where theta = 2*PI / N // N == 2^k static vector< complex > do_fft(vector< complex > A, bool inverse){ const T PI = atan2(1, 0) * 2.0; const complex j = complex(0,1); // 0 + j*1.0 (j is the imaginary unit) int N = A.size(); int k = 0; // N = 2^k while(N>>k > 0) k++; for(int bit=0; bit>= 1){ rbit |= tmp_bit & 1; } rbit >>= 1; if(bit < rbit){ swap(A[bit], A[rbit]); } } int dist = 1; T theta = -PI; if(inverse) theta = -theta; for(int level = 1; level W_n = exp(j * theta); complex W(1,0); for(int x=0; x < (1< tmp = A[ y+dist ] * W; A[ y+dist ] = A[ y ] - tmp; A[ y ] += tmp; } W *= W_n; } dist <<= 1; theta *= 0.5; } if(inverse){ T k = 1.0/N; for(int i=0; i static void vec_resize(vector &A, const U val){ int n = A.size(); int k = 1; while(n > k) k<<=1; A.resize(k, val); } public: Fast_Fourier_Transform(){}; static vector< complex > FFT(vector< complex > A){ vec_resize>(A, complex(0,0)); return do_fft(A, false); } static vector< complex > IFFT(vector< complex > A){ //vec_resize>(A, complex(0,0)); return do_fft(A, true); } static vector< complex > FFT(vector A){ vec_resize(A, 0); vector> B(A.size()); for(int i=0; i(A[i], 0); } return do_fft(B, false); } static vector< complex > FFT(vector A){ vec_resize(A, T(0)); vector> B(A.size()); for(int i=0; i(A[i], 0); } return do_fft(B, false); } static vector round(vector> &&A){ vector ret(A.size()); for(int i=0; i C | C[i] = ΣA[i]B[j] static vector> convolution(vector &A, vector &B){ //reverse(B.begin(), B.end()); int n = max(A.size(), B.size()); //A.resize(n, 0); //B.resize(n, 0); auto A_FFT = FFT(A); auto B_FFT = FFT(B); for(int i=0; i> t; const int sz = 3000; vector> nCk(sz, vector(sz, 0)); nCk[0][0] = 1; for(int i=1; i0?nCk[i-1][j-1]:0)) % 9; } } vector B(nCk[sz-1].begin(), nCk[sz-1].end()); B.resize(1<<18); //reverse(B.begin(), B.end()); vector A(1<<18, 0); while(t--){ string s; cin >> s; bool zero = true; for(int i=0; i sz){ fill(A.begin(), A.end(), 0.0); for(int i=0; i::round( Fast_Fourier_Transform<>::convolution(A,B) ); string t; for(int i=0; i v(s.size()); for(int i=0; i