#include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=200, INF=1e9+5, mod=1e9+7; struct modint{ int n=0; modint(){} modint(ll x){ n=x%mod; if(n<0)n+=mod; } operator int(){ return n; } modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;} modint operator+=(modint a){return (*this)=(*this)+a;} modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;} modint operator-=(modint a){return (*this)=(*this)-a;} modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;} modint operator*=(modint a){return (*this)=(*this)*a;} modint pot(const ll &m)const{ modint a(1); if(m==0)return a; if(m==1)return (*this); a=(*this).pot(m/2); a*=a; return a*((*this).pot(m&1)); } modint odw(){ return (*this).pot(mod-2); } modint operator/(modint a){return (*this)*a.odw();} modint operator/=(modint a){return (*this)=(*this)/a;} bool operator==(modint a){return a.n==n;} friend ostream& operator<<(ostream& os, modint m) { return os << m.n; } }; int licz(vi &V){ //deb(V); int k=1; for(int j=0; j+1<V.size(); j++){ if(V[j]!=V[j+1]){ k=size(V)-j; break; } } return k; } int eval(vi v){ int m=v.size(); int k=licz(v); int cnt=0; for(int i=m-1; i>=0; i--){ int j=min(m-i, k); if(v[i])cnt+=(1<<(j+m-k)); else cnt-=(1<<(j+m-k)); } return cnt; } const int MAXBIT=24, MAXSTOS=32, MAXSUM=780, MAXSUM2=500; ll dp[2][MAXSTOS+1][2*MAXSUM][2*MAXSTOS+5]; int newton[MAXSTOS+1][MAXSTOS+1], pot[MAXSTOS+1]; modint ans[MAXSTOS+1]; int newton2[MAXSTOS+1][MAXSTOS+1][MAXSTOS+1][MAXSTOS+1];; int maksotw, ilebitow; int main(){ pot[0]=1; for(int i=0; i<=MAXSTOS; i++){ if(i){ pot[i]=(pot[i-1]*2)%mod; } for(int j=0; j<=i; j++){ if(j==0 || j==i)newton[i][j]=1; else{ newton[i][j]=(newton[i-1][j]+newton[i-1][j-1])%mod; } } } int n, m, k; cin>>n>>m>>k; for(int i=0; i<=n-k; i++){ for(int j=0; j<=i; j++){ for(int l=0; l<=j; l++){ for(int l2=0; l2<=n-k-i+j; l2++){ newton2[i][j][l][l2]= (((newton[i][j]*1ll*newton[n-k-i+j][l2])%mod)*1ll*newton[j][l])%mod; } } } } int start=0; for(int i=0; i<k; i++){ string s; cin>>s; vi V;for(char &c:s)V.pb(c=='C'); start+=eval(V); } vector<int> startbit(m+1); for(int i=1; i<m; i++){ if(start%(1<<(i+1))){ startbit[i]=1; start-=(1<<i); } } startbit[m]=start/(1<<m); deb(startbit); maksotw=n-k;//takie n ilebitow=m;//takie m dp[1][0][MAXSUM][MAXSTOS]=1; for(int bit=1; bit<=ilebitow; bit++){ int bit2=bit&1; int bit3=(bit+1)&1; for(int ileotw=0; ileotw<=maksotw; ileotw++){ for(int bilans=0; bilans<2*MAXSUM; bilans++){ for(int spadek=-MAXSTOS; spadek<=MAXSTOS; spadek++){ ll dpval=dp[bit2][ileotw][bilans][spadek+MAXSTOS]; if(!dpval)continue; dpval%=mod; //deb(bit,ileotw, bilans-MAXSUM, spadek, dpval); if(bit==m){ int todo=bilans-MAXSUM+spadek+startbit[m]; if(todo%ilebitow)continue; todo/=ilebitow; //deb(todo); for(int otw=ileotw+(todo&1); otw<=maksotw; otw+=2){ int plus=(-todo+otw-ileotw)/2, minus=(todo+otw-ileotw)/2; if(plus<0 || minus<0)continue; //deb(otw, plus, minus); ans[otw]+=modint(dpval*(newton[plus+minus][minus]*1ll*newton[maksotw-ileotw][otw-ileotw]%mod)); } } else if(bit==m-1){ int todo=spadek+startbit[bit]; for(int nowe=(ileotw+todo)&1; ileotw+nowe<=maksotw; nowe+=2){ for(int plus=0; plus<=nowe; plus++){ dp[bit3][ileotw+nowe] [bilans+(todo+ileotw+(2*plus-nowe)*(2*bit-1))/2][MAXSTOS] +=(dpval*((newton[maksotw-ileotw][nowe]*1ll*newton[nowe][plus])%mod))%mod; } } } else{ int todo=spadek+startbit[bit]; //deb(todo); for(int nowe=(ileotw+todo)&1; ileotw+nowe<=maksotw; nowe+=2){ for(int plus=0; plus<=nowe; plus++){ int bilans2=bilans+((2*plus-nowe)*(2*bit-1)-nowe)/2; int spadek2=(-ileotw-nowe+todo)/2+MAXSTOS; for(int plus2=0; plus2+plus2<ileotw+nowe; plus2++){ int a=(dpval*newton2[maksotw-ileotw][nowe][plus][plus2])%mod; dp[bit3][ileotw+nowe][bilans2][plus2+spadek2]+=a; dp[bit3][ileotw+nowe][bilans2][spadek2+ileotw+nowe-plus2]+=a; } if(!((ileotw+nowe)&1)){ dp[bit3][ileotw+nowe][bilans2][todo/2+MAXSTOS]+= (dpval*newton2[maksotw-ileotw][nowe][plus][(ileotw+nowe)/2])%mod; } } } } dp[bit2][ileotw][bilans][spadek+MAXSTOS]=0; } } } } for(int i=0; i<=maksotw; i++){ ans[i]/=newton[maksotw][i]; cout<<ans[i]<<" "; } }
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 | #include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=200, INF=1e9+5, mod=1e9+7; struct modint{ int n=0; modint(){} modint(ll x){ n=x%mod; if(n<0)n+=mod; } operator int(){ return n; } modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;} modint operator+=(modint a){return (*this)=(*this)+a;} modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;} modint operator-=(modint a){return (*this)=(*this)-a;} modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;} modint operator*=(modint a){return (*this)=(*this)*a;} modint pot(const ll &m)const{ modint a(1); if(m==0)return a; if(m==1)return (*this); a=(*this).pot(m/2); a*=a; return a*((*this).pot(m&1)); } modint odw(){ return (*this).pot(mod-2); } modint operator/(modint a){return (*this)*a.odw();} modint operator/=(modint a){return (*this)=(*this)/a;} bool operator==(modint a){return a.n==n;} friend ostream& operator<<(ostream& os, modint m) { return os << m.n; } }; int licz(vi &V){ //deb(V); int k=1; for(int j=0; j+1<V.size(); j++){ if(V[j]!=V[j+1]){ k=size(V)-j; break; } } return k; } int eval(vi v){ int m=v.size(); int k=licz(v); int cnt=0; for(int i=m-1; i>=0; i--){ int j=min(m-i, k); if(v[i])cnt+=(1<<(j+m-k)); else cnt-=(1<<(j+m-k)); } return cnt; } const int MAXBIT=24, MAXSTOS=32, MAXSUM=780, MAXSUM2=500; ll dp[2][MAXSTOS+1][2*MAXSUM][2*MAXSTOS+5]; int newton[MAXSTOS+1][MAXSTOS+1], pot[MAXSTOS+1]; modint ans[MAXSTOS+1]; int newton2[MAXSTOS+1][MAXSTOS+1][MAXSTOS+1][MAXSTOS+1];; int maksotw, ilebitow; int main(){ pot[0]=1; for(int i=0; i<=MAXSTOS; i++){ if(i){ pot[i]=(pot[i-1]*2)%mod; } for(int j=0; j<=i; j++){ if(j==0 || j==i)newton[i][j]=1; else{ newton[i][j]=(newton[i-1][j]+newton[i-1][j-1])%mod; } } } int n, m, k; cin>>n>>m>>k; for(int i=0; i<=n-k; i++){ for(int j=0; j<=i; j++){ for(int l=0; l<=j; l++){ for(int l2=0; l2<=n-k-i+j; l2++){ newton2[i][j][l][l2]= (((newton[i][j]*1ll*newton[n-k-i+j][l2])%mod)*1ll*newton[j][l])%mod; } } } } int start=0; for(int i=0; i<k; i++){ string s; cin>>s; vi V;for(char &c:s)V.pb(c=='C'); start+=eval(V); } vector<int> startbit(m+1); for(int i=1; i<m; i++){ if(start%(1<<(i+1))){ startbit[i]=1; start-=(1<<i); } } startbit[m]=start/(1<<m); deb(startbit); maksotw=n-k;//takie n ilebitow=m;//takie m dp[1][0][MAXSUM][MAXSTOS]=1; for(int bit=1; bit<=ilebitow; bit++){ int bit2=bit&1; int bit3=(bit+1)&1; for(int ileotw=0; ileotw<=maksotw; ileotw++){ for(int bilans=0; bilans<2*MAXSUM; bilans++){ for(int spadek=-MAXSTOS; spadek<=MAXSTOS; spadek++){ ll dpval=dp[bit2][ileotw][bilans][spadek+MAXSTOS]; if(!dpval)continue; dpval%=mod; //deb(bit,ileotw, bilans-MAXSUM, spadek, dpval); if(bit==m){ int todo=bilans-MAXSUM+spadek+startbit[m]; if(todo%ilebitow)continue; todo/=ilebitow; //deb(todo); for(int otw=ileotw+(todo&1); otw<=maksotw; otw+=2){ int plus=(-todo+otw-ileotw)/2, minus=(todo+otw-ileotw)/2; if(plus<0 || minus<0)continue; //deb(otw, plus, minus); ans[otw]+=modint(dpval*(newton[plus+minus][minus]*1ll*newton[maksotw-ileotw][otw-ileotw]%mod)); } } else if(bit==m-1){ int todo=spadek+startbit[bit]; for(int nowe=(ileotw+todo)&1; ileotw+nowe<=maksotw; nowe+=2){ for(int plus=0; plus<=nowe; plus++){ dp[bit3][ileotw+nowe] [bilans+(todo+ileotw+(2*plus-nowe)*(2*bit-1))/2][MAXSTOS] +=(dpval*((newton[maksotw-ileotw][nowe]*1ll*newton[nowe][plus])%mod))%mod; } } } else{ int todo=spadek+startbit[bit]; //deb(todo); for(int nowe=(ileotw+todo)&1; ileotw+nowe<=maksotw; nowe+=2){ for(int plus=0; plus<=nowe; plus++){ int bilans2=bilans+((2*plus-nowe)*(2*bit-1)-nowe)/2; int spadek2=(-ileotw-nowe+todo)/2+MAXSTOS; for(int plus2=0; plus2+plus2<ileotw+nowe; plus2++){ int a=(dpval*newton2[maksotw-ileotw][nowe][plus][plus2])%mod; dp[bit3][ileotw+nowe][bilans2][plus2+spadek2]+=a; dp[bit3][ileotw+nowe][bilans2][spadek2+ileotw+nowe-plus2]+=a; } if(!((ileotw+nowe)&1)){ dp[bit3][ileotw+nowe][bilans2][todo/2+MAXSTOS]+= (dpval*newton2[maksotw-ileotw][nowe][plus][(ileotw+nowe)/2])%mod; } } } } dp[bit2][ileotw][bilans][spadek+MAXSTOS]=0; } } } } for(int i=0; i<=maksotw; i++){ ans[i]/=newton[maksotw][i]; cout<<ans[i]<<" "; } } |