//#ifdef DEBUG //#define _GLIBCXX_DEBUG //#endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; typedef long double ld; const int mod = (int)1e9 + 7; int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } int n, m, k; int add_carry[45]; void upd(string s) { int to_mult = 1; if (s[0] == 'N') { to_mult *= -1; for (char& c : s) { c ^= ('N' ^ 'C'); } } for (int z = 0; z < m; z++) { if (s[z] == 'N') { assert(z > 0); add_carry[0] += z * to_mult; for (int t = z; t < m; t++) { if (s[t] == 'N') { add_carry[t - z + 1] -= to_mult; } else { add_carry[t - z + 1] += to_mult; } } return; } } add_carry[0] += m * to_mult; } const int maxN = 34; int inv[maxN]; int fact[maxN]; int invfact[maxN]; const int maxM = 27; const int SHIFT_A = 4 * maxN * maxM; const int SHIFT_B = 2 * maxN; //where, sum of 2 * add[0] + add_1[0], already used, cur_carry int dp[maxN][8 * maxN * maxM][4 * maxN]; int ndp[maxN][8 * maxN * maxM][4 * maxN]; //34 * 27 * 8 * 34 * 27 * 4 * 34 int c[maxN][maxN]; void upd(int& a, int b) { a += b; if (a >= mod) a -= mod; } int help[maxN][8 * maxN * maxM][4 * maxN]; int P[maxN][8 * maxN * maxM]; int ans[38]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif cin >> n >> m >> k; for (int i = 0; i < k; i++) { string s; cin >> s; upd(s); } fact[0] = invfact[0] = fact[1] = invfact[1] = inv[1] = 1; for (int i = 2; i < maxN; i++) { fact[i] = mult(i, fact[i - 1]); inv[i] = mult(inv[mod % i], sub(0, mod / i)); invfact[i] = mult(invfact[i - 1], inv[i]); } c[0][0] = 1; for (int i = 1; i < maxN; i++) { c[i][0] = 1; for (int j = 1; j <= i; j++) { c[i][j] = sum(c[i - 1][j], c[i - 1][j - 1]); } } dp[0][2 * add_carry[0] + add_carry[1] + SHIFT_A][SHIFT_B] = 1; int lft = n - k; int MX = 0; for (int i = m - 1; i > 0; i--) { memset(help, 0, sizeof help); memset(ndp, 0, sizeof ndp); for (int used = 0; used <= lft; used++) { for (int add_0 = -4 * m * n; add_0 <= 4 * m * n; add_0++) { for (int cur_carry = -2 * n; cur_carry <= 2 * n; cur_carry++) { if (!dp[used][add_0 + SHIFT_A][cur_carry + SHIFT_B]) continue; MX = max(MX, add_0); for (int take_pos = 0; take_pos <= lft - used; take_pos++) { for (int take_neg = 0; take_neg + take_pos <= lft - used; take_neg++) { int X = (take_pos - take_neg) * (2 * (m - i) - 1); int new_S = (used + take_pos + take_neg); int nadd_0 = add_0 + X + SHIFT_A; upd(help[new_S][nadd_0][cur_carry + SHIFT_B], mult(dp[used][add_0 + SHIFT_A][cur_carry + SHIFT_B], mult(invfact[take_pos], invfact[take_neg]))); } } } } } for (int used = 0; used <= lft; used++) { for (int add_0 = -4 * m * n; add_0 <= 4 * m * n; add_0++) { for (int cur_carry = -2 * n; cur_carry <= 2 * n; cur_carry++) { if (!help[used][add_0 + SHIFT_A][cur_carry + SHIFT_B]) continue; if (i > 1) { for (int z = 0; z <= used; z++) { int ncarry = cur_carry + add_carry[i] + z - (used - z); if (ncarry % 2 != 0) continue; upd(ndp[used][add_0 + SHIFT_A][ncarry / 2 + SHIFT_B], mult(c[used][z], help[used][add_0 + SHIFT_A][cur_carry + SHIFT_B])); } } else { upd(P[used][add_0 + cur_carry + SHIFT_A], help[used][add_0 + SHIFT_A][cur_carry + SHIFT_B]); } } } } memcpy(dp, ndp, sizeof(dp)); } if (m == 1) { upd(P[0][2 * add_carry[0] + SHIFT_A], 1); } for (int used = 0; used <= lft; used++) { for (int S = -4 * n * m; S <= 4 * n * m; S++) { if (!P[used][S + SHIFT_A]) continue; for (int take_pos = 0; take_pos <= lft - used; take_pos++) { for (int take_neg = 0; take_neg + take_pos <= lft - used; take_neg++) { int U = S + (take_pos - take_neg) * 2 * m; if (U == 0) { upd(ans[used + take_pos + take_neg], mult(P[used][S + SHIFT_A], mult(invfact[take_pos], invfact[take_neg]))); } } } } } for (int z = 0; z <= n - k; z++) { cout << mult(fact[z], ans[z]) << " "; } 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 166 167 168 169 170 171 172 173 174 | //#ifdef DEBUG //#define _GLIBCXX_DEBUG //#endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; typedef long double ld; const int mod = (int)1e9 + 7; int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } int n, m, k; int add_carry[45]; void upd(string s) { int to_mult = 1; if (s[0] == 'N') { to_mult *= -1; for (char& c : s) { c ^= ('N' ^ 'C'); } } for (int z = 0; z < m; z++) { if (s[z] == 'N') { assert(z > 0); add_carry[0] += z * to_mult; for (int t = z; t < m; t++) { if (s[t] == 'N') { add_carry[t - z + 1] -= to_mult; } else { add_carry[t - z + 1] += to_mult; } } return; } } add_carry[0] += m * to_mult; } const int maxN = 34; int inv[maxN]; int fact[maxN]; int invfact[maxN]; const int maxM = 27; const int SHIFT_A = 4 * maxN * maxM; const int SHIFT_B = 2 * maxN; //where, sum of 2 * add[0] + add_1[0], already used, cur_carry int dp[maxN][8 * maxN * maxM][4 * maxN]; int ndp[maxN][8 * maxN * maxM][4 * maxN]; //34 * 27 * 8 * 34 * 27 * 4 * 34 int c[maxN][maxN]; void upd(int& a, int b) { a += b; if (a >= mod) a -= mod; } int help[maxN][8 * maxN * maxM][4 * maxN]; int P[maxN][8 * maxN * maxM]; int ans[38]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif cin >> n >> m >> k; for (int i = 0; i < k; i++) { string s; cin >> s; upd(s); } fact[0] = invfact[0] = fact[1] = invfact[1] = inv[1] = 1; for (int i = 2; i < maxN; i++) { fact[i] = mult(i, fact[i - 1]); inv[i] = mult(inv[mod % i], sub(0, mod / i)); invfact[i] = mult(invfact[i - 1], inv[i]); } c[0][0] = 1; for (int i = 1; i < maxN; i++) { c[i][0] = 1; for (int j = 1; j <= i; j++) { c[i][j] = sum(c[i - 1][j], c[i - 1][j - 1]); } } dp[0][2 * add_carry[0] + add_carry[1] + SHIFT_A][SHIFT_B] = 1; int lft = n - k; int MX = 0; for (int i = m - 1; i > 0; i--) { memset(help, 0, sizeof help); memset(ndp, 0, sizeof ndp); for (int used = 0; used <= lft; used++) { for (int add_0 = -4 * m * n; add_0 <= 4 * m * n; add_0++) { for (int cur_carry = -2 * n; cur_carry <= 2 * n; cur_carry++) { if (!dp[used][add_0 + SHIFT_A][cur_carry + SHIFT_B]) continue; MX = max(MX, add_0); for (int take_pos = 0; take_pos <= lft - used; take_pos++) { for (int take_neg = 0; take_neg + take_pos <= lft - used; take_neg++) { int X = (take_pos - take_neg) * (2 * (m - i) - 1); int new_S = (used + take_pos + take_neg); int nadd_0 = add_0 + X + SHIFT_A; upd(help[new_S][nadd_0][cur_carry + SHIFT_B], mult(dp[used][add_0 + SHIFT_A][cur_carry + SHIFT_B], mult(invfact[take_pos], invfact[take_neg]))); } } } } } for (int used = 0; used <= lft; used++) { for (int add_0 = -4 * m * n; add_0 <= 4 * m * n; add_0++) { for (int cur_carry = -2 * n; cur_carry <= 2 * n; cur_carry++) { if (!help[used][add_0 + SHIFT_A][cur_carry + SHIFT_B]) continue; if (i > 1) { for (int z = 0; z <= used; z++) { int ncarry = cur_carry + add_carry[i] + z - (used - z); if (ncarry % 2 != 0) continue; upd(ndp[used][add_0 + SHIFT_A][ncarry / 2 + SHIFT_B], mult(c[used][z], help[used][add_0 + SHIFT_A][cur_carry + SHIFT_B])); } } else { upd(P[used][add_0 + cur_carry + SHIFT_A], help[used][add_0 + SHIFT_A][cur_carry + SHIFT_B]); } } } } memcpy(dp, ndp, sizeof(dp)); } if (m == 1) { upd(P[0][2 * add_carry[0] + SHIFT_A], 1); } for (int used = 0; used <= lft; used++) { for (int S = -4 * n * m; S <= 4 * n * m; S++) { if (!P[used][S + SHIFT_A]) continue; for (int take_pos = 0; take_pos <= lft - used; take_pos++) { for (int take_neg = 0; take_neg + take_pos <= lft - used; take_neg++) { int U = S + (take_pos - take_neg) * 2 * m; if (U == 0) { upd(ans[used + take_pos + take_neg], mult(P[used][S + SHIFT_A], mult(invfact[take_pos], invfact[take_neg]))); } } } } } for (int z = 0; z <= n - k; z++) { cout << mult(fact[z], ans[z]) << " "; } return 0; } |