// [5A] Monety, Kamil Debowski #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } int getScore(const string& s) { int total = 0; int expo = (int) s.length() - 1; bool same = true; for (char c : s) { if (c != s[0]) { same = false; } if (!same) expo--; total += (1 << expo) * (c == 'A' ? +1 : -1); } return total; } long long target; long long answer; int n, m; const int MOD = 1'000'000'007; void solve() { if (m == 1) { vector<vector<int>> C(n + 1, vector<int>(n + 1)); for (int i = 0; i <= n; i++) { C[i][0] = C[i][i] = 1; for (int j = 1; j < i; j++) { C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD; } } for (int j = 0; j <= n; j++) { if (j - (n - j) == target) { answer = (answer + C[n][j]) % MOD; } } return; } vector<vector<vector<long long>>> empty(n+1); for (int started = 0; started <= n; started++) { empty[started].resize(4 * started + 1, vector<long long>(2 * m * started + 1)); } auto dp = empty; auto new_dp = empty; dp[0][0][0] = 1; for (int bit = 0; bit <= m - 2; bit++) { for (int i = 0; i <= n; i++) { for (int started = 0; started <= n; started++) { // hack - skip if can't reach the target vector<bool> skip((2 * bit + 1) * started + 1); for (int sumInt = 0; sumInt < (int) skip.size(); sumInt++) { // int max_change = 2 * started + (n - started) * 2 * m; int max_change = started + (n - started) * 2 * m; if (abs(sumInt - (target / (1 << (m - 2)))) > max_change && abs(-sumInt - (target / (1 << (m - 2)))) > max_change) { skip[sumInt] = true; } } for (int sum = -2 * started; sum <= 2 * started; sum++) { if (bit == m - 2) { for (int sumInt = 0; sumInt <= 2 * m * started; sumInt++) { long long& me = dp[started][sum+2*started][sumInt]; if (!me) { continue; } me %= MOD; assert(abs(sum) <= n); auto add = [&](long long& x) { x += me; }; if (i == n) { assert(started == n); if (sum + sumInt == target / (1 << (m - 2))) { add(answer); } if (sumInt != 0 && sum - sumInt == target / (1 << (m - 2))) { add(answer); } } else if (i < started) { add(new_dp[started][sum+2*started][sumInt]); } else { for (int a : {-1, 1}) { for (int b : {2 * m, 2 * m - 3}) { debug() << imie(bit) imie(i+1) imie(started) imie(sum) imie(sumInt+a*b); // add(dp[bit][i+1][started+1][sum][sumInt + a * b]); for (int x : {sumInt + a * b, -sumInt + a * b}) { if (x >= 0) { add(new_dp[started+1][sum+2*(started+1)][x]); } if (sumInt == 0) break; } } } } me = 0; } } else { // bit < m - 2 for (int sumInt = started % 2; sumInt <= (2 * bit + 1) * started; sumInt += 2) { long long& me = dp[started][sum+2*started][sumInt]; if (!me) { continue; } me %= MOD; // hack if (skip[sumInt]) { me = 0; continue; } auto add = [&](long long& x) { x += me; }; if (i == n) { if (abs(sum) % 2 == bool(abs(target) & (1 << bit))) { int x = sum; if (x % 2 && target < 0) { x++; } add(new_dp[started][(x>>1)+2*started][sumInt]); } } else if (i < started) { add(new_dp[started][sum+1+2*started][sumInt]); add(new_dp[started][sum-1+2*started][sumInt]); // never negative??? } else { add(new_dp[started][sum+2*started][sumInt]); int whole = 2 * bit + 1; for (int b : {-1, +1}) { // add(dp[bit][i+1][started+1][sum+a][sumInt+b*whole]); for (int x : {sumInt + b*whole, -sumInt + b*whole}) { if (x >= 0) { for (int a : {-1, +1}) { add(new_dp[started+1][sum+a+2*(started+1)][x]); } } if (sumInt == 0) break; } } } me = 0; } } } } swap(dp, new_dp); } } } int main() { int k; cin >> n >> m >> k; n -= k; for (int rep = 0; rep < k; rep++) { string s; cin >> s; for (char& c : s) { if (c == 'C') c = 'A'; if (c == 'N') c = 'B'; } target -= getScore(s); // debug() << imie(s) imie(getScore(s)); } debug() << imie(target); int N = n; for (n = 0; n <= N; n++) { answer = 0; solve(); cout << answer % MOD << " "; } cout << endl; }
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 | // [5A] Monety, Kamil Debowski #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } int getScore(const string& s) { int total = 0; int expo = (int) s.length() - 1; bool same = true; for (char c : s) { if (c != s[0]) { same = false; } if (!same) expo--; total += (1 << expo) * (c == 'A' ? +1 : -1); } return total; } long long target; long long answer; int n, m; const int MOD = 1'000'000'007; void solve() { if (m == 1) { vector<vector<int>> C(n + 1, vector<int>(n + 1)); for (int i = 0; i <= n; i++) { C[i][0] = C[i][i] = 1; for (int j = 1; j < i; j++) { C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD; } } for (int j = 0; j <= n; j++) { if (j - (n - j) == target) { answer = (answer + C[n][j]) % MOD; } } return; } vector<vector<vector<long long>>> empty(n+1); for (int started = 0; started <= n; started++) { empty[started].resize(4 * started + 1, vector<long long>(2 * m * started + 1)); } auto dp = empty; auto new_dp = empty; dp[0][0][0] = 1; for (int bit = 0; bit <= m - 2; bit++) { for (int i = 0; i <= n; i++) { for (int started = 0; started <= n; started++) { // hack - skip if can't reach the target vector<bool> skip((2 * bit + 1) * started + 1); for (int sumInt = 0; sumInt < (int) skip.size(); sumInt++) { // int max_change = 2 * started + (n - started) * 2 * m; int max_change = started + (n - started) * 2 * m; if (abs(sumInt - (target / (1 << (m - 2)))) > max_change && abs(-sumInt - (target / (1 << (m - 2)))) > max_change) { skip[sumInt] = true; } } for (int sum = -2 * started; sum <= 2 * started; sum++) { if (bit == m - 2) { for (int sumInt = 0; sumInt <= 2 * m * started; sumInt++) { long long& me = dp[started][sum+2*started][sumInt]; if (!me) { continue; } me %= MOD; assert(abs(sum) <= n); auto add = [&](long long& x) { x += me; }; if (i == n) { assert(started == n); if (sum + sumInt == target / (1 << (m - 2))) { add(answer); } if (sumInt != 0 && sum - sumInt == target / (1 << (m - 2))) { add(answer); } } else if (i < started) { add(new_dp[started][sum+2*started][sumInt]); } else { for (int a : {-1, 1}) { for (int b : {2 * m, 2 * m - 3}) { debug() << imie(bit) imie(i+1) imie(started) imie(sum) imie(sumInt+a*b); // add(dp[bit][i+1][started+1][sum][sumInt + a * b]); for (int x : {sumInt + a * b, -sumInt + a * b}) { if (x >= 0) { add(new_dp[started+1][sum+2*(started+1)][x]); } if (sumInt == 0) break; } } } } me = 0; } } else { // bit < m - 2 for (int sumInt = started % 2; sumInt <= (2 * bit + 1) * started; sumInt += 2) { long long& me = dp[started][sum+2*started][sumInt]; if (!me) { continue; } me %= MOD; // hack if (skip[sumInt]) { me = 0; continue; } auto add = [&](long long& x) { x += me; }; if (i == n) { if (abs(sum) % 2 == bool(abs(target) & (1 << bit))) { int x = sum; if (x % 2 && target < 0) { x++; } add(new_dp[started][(x>>1)+2*started][sumInt]); } } else if (i < started) { add(new_dp[started][sum+1+2*started][sumInt]); add(new_dp[started][sum-1+2*started][sumInt]); // never negative??? } else { add(new_dp[started][sum+2*started][sumInt]); int whole = 2 * bit + 1; for (int b : {-1, +1}) { // add(dp[bit][i+1][started+1][sum+a][sumInt+b*whole]); for (int x : {sumInt + b*whole, -sumInt + b*whole}) { if (x >= 0) { for (int a : {-1, +1}) { add(new_dp[started+1][sum+a+2*(started+1)][x]); } } if (sumInt == 0) break; } } } me = 0; } } } } swap(dp, new_dp); } } } int main() { int k; cin >> n >> m >> k; n -= k; for (int rep = 0; rep < k; rep++) { string s; cin >> s; for (char& c : s) { if (c == 'C') c = 'A'; if (c == 'N') c = 'B'; } target -= getScore(s); // debug() << imie(s) imie(getScore(s)); } debug() << imie(target); int N = n; for (n = 0; n <= N; n++) { answer = 0; solve(); cout << answer % MOD << " "; } cout << endl; } |