#include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; ll V = 1LL<<24; const long long MD = 1e9+7; template<long long MOD=MD> struct mdint { int d; mdint () {d=0;} mdint (long long _d) : d(_d%MOD){ if(d<0) d+=MOD; }; friend mdint& operator+=(mdint& a, const mdint& o) { a.d+=o.d; if(a.d>=MOD) a.d-=MOD; return a; } friend mdint& operator-=(mdint& a, const mdint& o) { a.d-=o.d; if(a.d<0) a.d+=MOD; return a; } friend mdint& operator*=(mdint& a, const mdint& o) { return a = mdint((ll)a.d*o.d); } mdint operator*(const mdint& o) const { mdint res = *this; res*=o; return res; } mdint operator+(const mdint& o) const { mdint res = *this; res+=o; return res; } mdint operator-(const mdint& o) const { mdint res = *this; res-=o; return res; } mdint operator^(long long b) const { mdint tmp = 1; mdint power = *this; while(b) { if(b&1) { tmp = tmp*power; } power = power*power; b/=2; } return tmp; } friend mdint operator/=(mdint& a, const mdint& o) { a *= (o^(MOD-2)); return a; } mdint operator/(const mdint& o) { mdint res = *this; res/=o; return res; } bool operator==(const mdint& o) { return d==o.d;} bool operator!=(const mdint& o) { return d!=o.d;} friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;} friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;} }; using mint = mdint<MD>; int read() { string s; cin >> s; int res=0; for(int j=0;j<s.size();++j) { if(s[j]=='C') { res+=1<<j; } } return res; } int m; ll calc(int x) { ll A = V; bool last=0; ll ans=0; bool done=0; for(int j=0;j<m;++j) { bool cur=0; if(x & (1<<j)) { cur=1; } if(j and cur!=last) { done=1; } if(done) { A/=2; } if(cur) ans+=A; else ans-=A; last=cur; } return ans; } typedef complex<double> C; typedef vector<double> vd; #define rep(i,a,b) for(int i=a;i<b;++i) #define sz(x) int(x.size()) void fft(vector<C>& a) { int n = sz(a), L = 31 - __builtin_clz(n); static vector<complex<long double>> R(2, 1); static vector<C> rt(2, 1); // (^ 10% faster if double) for (static int k = 2; k < n; k *= 2) { R.resize(n); rt.resize(n); auto x = polar(1.0L, acos(-1.0L) / k); rep(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2]; } vi rev(n); rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2; rep(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int k = 1; k < n; k *= 2) for (int i = 0; i < n; i += 2 * k) rep(j,0,k) { // C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) /// include-line auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k]; /// exclude-line C z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]); /// exclude-line a[i + j + k] = a[i + j] - z; a[i + j] += z; } } vd conv(const vd& a, const vd& b) { if (a.empty() || b.empty()) return {}; vd res(sz(a) + sz(b) - 1); int L = 32 - __builtin_clz(sz(res)), n = 1 << L; vector<C> in(n), out(n); copy(all(a), begin(in)); rep(i,0,sz(b)) in[i].imag(b[i]); fft(in); for (C& x : in) x *= x; rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]); fft(out); rep(i,0,sz(res)) res[i] = imag(out[i]) / (4 * n); return res; } typedef vector<ll> vl; template<int M> vl convMod(const vl &a, const vl &b) { if (a.empty() || b.empty()) return {}; vl res(sz(a) + sz(b) - 1); int B=32-__builtin_clz(sz(res)), n=1<<B, cut=int(sqrt(M)); vector<C> L(n), R(n), outs(n), outl(n); rep(i,0,sz(a)) L[i] = C((int)a[i] / cut, (int)a[i] % cut); rep(i,0,sz(b)) R[i] = C((int)b[i] / cut, (int)b[i] % cut); fft(L), fft(R); rep(i,0,n) { int j = -i & (n - 1); outl[j] = (L[i] + conj(L[j])) * R[i] / (2.0 * n); outs[j] = (L[i] - conj(L[j])) * R[i] / (2.0 * n) / 1i; } fft(outl), fft(outs); rep(i,0,sz(res)) { ll av = ll(real(outl[i])+.5), cv = ll(imag(outs[i])+.5); ll bv = ll(imag(outl[i])+.5) + ll(real(outs[i])+.5); res[i] = ((av % M * cut + bv) % M * cut + cv) % M; } return res; } auto myconv(vector<pi> a, vector<pi> b) { int mn = min_element(all(a))->first, mn2 = min_element(all(b))->first; vl aa,bb; for(auto [v,x] : a) { while(aa.size()<=v-mn) { aa.push_back(0); } aa[v-mn]+=x; aa[v-mn]%=MD; } for(auto [v,x] : b) { while(bb.size()<=v-mn2) { bb.push_back(0); } bb[v-mn2]+=x; bb[v-mn2]%=MD; } auto res= convMod<MD>(aa,bb); vector<pi> ans; for(int i=0;i<res.size();++i) { if(res[i]) { ans.push_back({mn+mn2+i,res[i]}); } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,k; cin >> n >> m >> k; V=1<<(m-1); unordered_map<ll,mint> dp; ll pref=0; for(int i=0;i<k;++i) { int j = read(); pref+=calc(j); } dp[pref]=1; vector<pi> vs; for(int j=0;j<1<<m;++j) { vs.push_back({calc(j),1}); } /* FFT? 2^12? 2^12 --> 123121211213 */ vector<pi> cur = {{pref,1}}; for(int j=k;j<=n;++j) { bool found=0; for(auto [k,v] : cur) { if(k==0) { cout << v << ' '; found=1; } } if(!found) cout << "0 "; // cout << dp[0] << ' '; // unordered_map<ll,mint> ndp; // for(auto [x,vv] : vs) { // for(auto [k,v] : dp) { // ndp[x+k]+=v; // } // } // swap(dp,ndp); cur = myconv(cur,vs); } }
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 | #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; ll V = 1LL<<24; const long long MD = 1e9+7; template<long long MOD=MD> struct mdint { int d; mdint () {d=0;} mdint (long long _d) : d(_d%MOD){ if(d<0) d+=MOD; }; friend mdint& operator+=(mdint& a, const mdint& o) { a.d+=o.d; if(a.d>=MOD) a.d-=MOD; return a; } friend mdint& operator-=(mdint& a, const mdint& o) { a.d-=o.d; if(a.d<0) a.d+=MOD; return a; } friend mdint& operator*=(mdint& a, const mdint& o) { return a = mdint((ll)a.d*o.d); } mdint operator*(const mdint& o) const { mdint res = *this; res*=o; return res; } mdint operator+(const mdint& o) const { mdint res = *this; res+=o; return res; } mdint operator-(const mdint& o) const { mdint res = *this; res-=o; return res; } mdint operator^(long long b) const { mdint tmp = 1; mdint power = *this; while(b) { if(b&1) { tmp = tmp*power; } power = power*power; b/=2; } return tmp; } friend mdint operator/=(mdint& a, const mdint& o) { a *= (o^(MOD-2)); return a; } mdint operator/(const mdint& o) { mdint res = *this; res/=o; return res; } bool operator==(const mdint& o) { return d==o.d;} bool operator!=(const mdint& o) { return d!=o.d;} friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;} friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;} }; using mint = mdint<MD>; int read() { string s; cin >> s; int res=0; for(int j=0;j<s.size();++j) { if(s[j]=='C') { res+=1<<j; } } return res; } int m; ll calc(int x) { ll A = V; bool last=0; ll ans=0; bool done=0; for(int j=0;j<m;++j) { bool cur=0; if(x & (1<<j)) { cur=1; } if(j and cur!=last) { done=1; } if(done) { A/=2; } if(cur) ans+=A; else ans-=A; last=cur; } return ans; } typedef complex<double> C; typedef vector<double> vd; #define rep(i,a,b) for(int i=a;i<b;++i) #define sz(x) int(x.size()) void fft(vector<C>& a) { int n = sz(a), L = 31 - __builtin_clz(n); static vector<complex<long double>> R(2, 1); static vector<C> rt(2, 1); // (^ 10% faster if double) for (static int k = 2; k < n; k *= 2) { R.resize(n); rt.resize(n); auto x = polar(1.0L, acos(-1.0L) / k); rep(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2]; } vi rev(n); rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2; rep(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int k = 1; k < n; k *= 2) for (int i = 0; i < n; i += 2 * k) rep(j,0,k) { // C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) /// include-line auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k]; /// exclude-line C z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]); /// exclude-line a[i + j + k] = a[i + j] - z; a[i + j] += z; } } vd conv(const vd& a, const vd& b) { if (a.empty() || b.empty()) return {}; vd res(sz(a) + sz(b) - 1); int L = 32 - __builtin_clz(sz(res)), n = 1 << L; vector<C> in(n), out(n); copy(all(a), begin(in)); rep(i,0,sz(b)) in[i].imag(b[i]); fft(in); for (C& x : in) x *= x; rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]); fft(out); rep(i,0,sz(res)) res[i] = imag(out[i]) / (4 * n); return res; } typedef vector<ll> vl; template<int M> vl convMod(const vl &a, const vl &b) { if (a.empty() || b.empty()) return {}; vl res(sz(a) + sz(b) - 1); int B=32-__builtin_clz(sz(res)), n=1<<B, cut=int(sqrt(M)); vector<C> L(n), R(n), outs(n), outl(n); rep(i,0,sz(a)) L[i] = C((int)a[i] / cut, (int)a[i] % cut); rep(i,0,sz(b)) R[i] = C((int)b[i] / cut, (int)b[i] % cut); fft(L), fft(R); rep(i,0,n) { int j = -i & (n - 1); outl[j] = (L[i] + conj(L[j])) * R[i] / (2.0 * n); outs[j] = (L[i] - conj(L[j])) * R[i] / (2.0 * n) / 1i; } fft(outl), fft(outs); rep(i,0,sz(res)) { ll av = ll(real(outl[i])+.5), cv = ll(imag(outs[i])+.5); ll bv = ll(imag(outl[i])+.5) + ll(real(outs[i])+.5); res[i] = ((av % M * cut + bv) % M * cut + cv) % M; } return res; } auto myconv(vector<pi> a, vector<pi> b) { int mn = min_element(all(a))->first, mn2 = min_element(all(b))->first; vl aa,bb; for(auto [v,x] : a) { while(aa.size()<=v-mn) { aa.push_back(0); } aa[v-mn]+=x; aa[v-mn]%=MD; } for(auto [v,x] : b) { while(bb.size()<=v-mn2) { bb.push_back(0); } bb[v-mn2]+=x; bb[v-mn2]%=MD; } auto res= convMod<MD>(aa,bb); vector<pi> ans; for(int i=0;i<res.size();++i) { if(res[i]) { ans.push_back({mn+mn2+i,res[i]}); } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,k; cin >> n >> m >> k; V=1<<(m-1); unordered_map<ll,mint> dp; ll pref=0; for(int i=0;i<k;++i) { int j = read(); pref+=calc(j); } dp[pref]=1; vector<pi> vs; for(int j=0;j<1<<m;++j) { vs.push_back({calc(j),1}); } /* FFT? 2^12? 2^12 --> 123121211213 */ vector<pi> cur = {{pref,1}}; for(int j=k;j<=n;++j) { bool found=0; for(auto [k,v] : cur) { if(k==0) { cout << v << ' '; found=1; } } if(!found) cout << "0 "; // cout << dp[0] << ' '; // unordered_map<ll,mint> ndp; // for(auto [x,vv] : vs) { // for(auto [k,v] : dp) { // ndp[x+k]+=v; // } // } // swap(dp,ndp); cur = myconv(cur,vs); } } |