#include <cstdio> #include <iostream> #include <vector> using namespace std; const int kDi[5] = { 1, 0, -1, 0, 0 }; const int kDj[5] = { 0, 1, 0, -1, 0 }; const int kModulus = 1000 * 1000 * 1000 + 7; void Normalize(long long* a) { (*a) %= kModulus; } void AddTo(long long a, long long* b) { Normalize(&a); Normalize(b); (*b) += a; Normalize(b); } void MultiplyBy(long long a, long long* b) { Normalize(&a); Normalize(b); (*b) *= a; Normalize(b); } long long Choose(const long long a, long long b) { vector<long long> n; for (int i = 0; i < b; ++i) n.push_back(a - i); while (b > 0) { for (int i = 0; i < n.size(); ++i) if (n[i] % b == 0) { n[i] /= b; break; } --b; } long long ret = 1; for (int i = 0; i < n.size(); ++i) MultiplyBy(n[i], &ret); return ret; } vector<vector<pair<int, int> > > piece_parts; vector<vector<vector<bool> > > piece_masks; void LearnPiece(const string& s1, const string& s2, const string& s3, const string& s4) { piece_parts.resize(piece_parts.size() + 1); for (int i = 0; i < s1.size(); ++i) if (s1[i] == 'x') piece_parts.back().push_back(make_pair(0, i)); for (int i = 0; i < s2.size(); ++i) if (s2[i] == 'x') piece_parts.back().push_back(make_pair(1, i)); for (int i = 0; i < s3.size(); ++i) if (s3[i] == 'x') piece_parts.back().push_back(make_pair(2, i)); for (int i = 0; i < s4.size(); ++i) if (s4[i] == 'x') piece_parts.back().push_back(make_pair(3, i)); piece_masks.resize(piece_masks.size() + 1); int x_max = 0, y_max = 0; for (int i = 0; i < piece_parts.back().size(); ++i) { x_max = max(x_max, piece_parts.back()[i].first ); y_max = max(y_max, piece_parts.back()[i].second); } piece_masks.back().resize(x_max + 5, vector<bool>(y_max + 5)); for (int i = 0; i < piece_parts.back().size(); ++i) for (int d = 0; d <= 4; ++d) { piece_masks.back()[kDi[d] + 2 + piece_parts.back()[i].first][kDj[d] + 2 + piece_parts.back()[i].second] = true; } } void LearnPiece(const string& s1, const string& s2, const string& s3) { LearnPiece(s1, s2, s3, ""); } void LearnPiece(const string& s1, const string& s2) { LearnPiece(s1, s2, ""); } void LearnPiece(const string& s1) { LearnPiece(s1, ""); } void LearnPieces() { // 0 LearnPiece("xx"); // 1 LearnPiece("x", "x"); // 2 LearnPiece("xxx"); // 3 LearnPiece("x", "x", "x"); // 4 LearnPiece("xx", "x "); // 5 LearnPiece("xx", " x"); // 6 LearnPiece("x ", "xx"); // 7 LearnPiece(" x", "xx"); // 8 LearnPiece("xxxx"); // 9 LearnPiece("x", "x", "x", "x"); // 10 LearnPiece("xxx", " x"); // 11 LearnPiece("x ", "xxx"); // 12 LearnPiece("xx", "x ", "x "); // 13 LearnPiece(" x", " x", "xx"); // 14 LearnPiece(" x", "xxx"); // 15 LearnPiece("xxx", "x "); // 16 LearnPiece("xx", " x", " x"); // 17 LearnPiece("x ", "x ", "xx"); // 18 LearnPiece("xxx", " x "); // 19 LearnPiece(" x ", "xxx"); // 20 LearnPiece(" x", "xx", " x"); // 21 LearnPiece("x ", "xx", "x "); // 22 LearnPiece("xx ", " xx"); // 23 LearnPiece(" x", "xx", "x "); LearnPiece("x ", "xx", " x"); LearnPiece("xx", "xx"); } int GetPieceCount() { return piece_parts.size(); } int GetPieceSize(const int k) { return piece_parts[k].size(); } vector<vector<int> > dist; bool PieceFits(const int x, const int y, const int k) { bool one_found = false, two_found = false; for (int i = 0; i < piece_parts[k].size(); ++i) { const int ii = x + piece_parts[k][i].first; const int jj = y + piece_parts[k][i].second; if (dist[ii][jj] == 0) return false; if (dist[ii][jj] == 3) return false; if (dist[ii][jj] == 1) one_found = true; if (dist[ii][jj] == 2) two_found = true; } return one_found && two_found; } vector<vector<long long> > ways; void ComputePieceDeltas(int x, int y, const int k, long long* C1, long long* W2) { x -= 2; y -= 2; *C1 = 0; *W2 = 0; for (int i = 0; i < piece_masks[k].size(); ++i) for (int j = 0; j < piece_masks[k][i].size(); ++j) if (piece_masks[k][i][j]) { const int ii = x + i; const int jj = y + j; // cerr << "masked " << ii << " " << jj << endl; if (dist[ii][jj] == 2) *W2 += ways[ii][jj]; else if (dist[ii][jj] == 1) { ++*C1; for (int d = 0; d < 4; ++d) if (!piece_masks[k][i + kDi[d]][j + kDj[d]]) { const int iii = ii + kDi[d]; const int jjj = jj + kDj[d]; if (dist[iii][jjj] == 2) ++*W2; } } // cerr << *C1 << ' ' << *W2 << endl; } } int n; vector<long long> answer; void ComputeStats() { long long C1 = 0, W2 = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if (dist[i][j] == 1) ++C1; if (dist[i][j] == 2) W2 += ways[i][j]; } answer.resize(6); for (int i = 0; i <= 5; ++i) AddTo(Choose(C1, i), &answer[i]); // cerr << "Fitting pieces" << endl; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) for (int k = 0; k < GetPieceCount(); ++k) if (PieceFits(i, j, k)) { // cerr << k << " fits at " << i << ' ' << j << endl; const int size = GetPieceSize(k); // cerr << "piece size " << size << endl; AddTo(1, &answer[size]); if (size < 4) { long long C1_delta, W2_delta; ComputePieceDeltas(i, j, k, &C1_delta, &W2_delta); // cerr << "deltas " << C1_delta << ' ' << W2_delta << endl; AddTo( C1 - C1_delta , &answer[size + 1]); AddTo(Choose(C1 - C1_delta, 2), &answer[size + 2]); AddTo( W2 - W2_delta , &answer[size + 2]); } // cerr << "answers"; // for (int i = 0; i < 5; ++i) cerr << ' ' << answer[i]; // cerr << endl; } } void Spread(const int s) { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (dist[i][j] == s) for (int d = 0; d < 4; ++d) { const int ii = i + kDi[d]; const int jj = j + kDj[d]; if (dist[ii][jj] == -1) dist[ii][jj] = s + 1; if (dist[ii][jj] == s + 1) ways[ii][jj] += ways[i][j]; } } int main() { LearnPieces(); int k; scanf("%d%d", &n, &k); dist.resize(n + 5, vector<int>(n + 5, 3)); ways.resize(n + 5, vector<long long>(n + 5)); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { char c; do { scanf("%c", &c); } while (c != '#' && c != '.'); if (c == '#') { dist[i][j] = 0; } else { dist[i][j] = -1; } } Spread(0); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (dist[i][j] == 1) ways[i][j] = 1; Spread(1); ComputeStats(); printf("%lld\n", answer[k]); 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 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | #include <cstdio> #include <iostream> #include <vector> using namespace std; const int kDi[5] = { 1, 0, -1, 0, 0 }; const int kDj[5] = { 0, 1, 0, -1, 0 }; const int kModulus = 1000 * 1000 * 1000 + 7; void Normalize(long long* a) { (*a) %= kModulus; } void AddTo(long long a, long long* b) { Normalize(&a); Normalize(b); (*b) += a; Normalize(b); } void MultiplyBy(long long a, long long* b) { Normalize(&a); Normalize(b); (*b) *= a; Normalize(b); } long long Choose(const long long a, long long b) { vector<long long> n; for (int i = 0; i < b; ++i) n.push_back(a - i); while (b > 0) { for (int i = 0; i < n.size(); ++i) if (n[i] % b == 0) { n[i] /= b; break; } --b; } long long ret = 1; for (int i = 0; i < n.size(); ++i) MultiplyBy(n[i], &ret); return ret; } vector<vector<pair<int, int> > > piece_parts; vector<vector<vector<bool> > > piece_masks; void LearnPiece(const string& s1, const string& s2, const string& s3, const string& s4) { piece_parts.resize(piece_parts.size() + 1); for (int i = 0; i < s1.size(); ++i) if (s1[i] == 'x') piece_parts.back().push_back(make_pair(0, i)); for (int i = 0; i < s2.size(); ++i) if (s2[i] == 'x') piece_parts.back().push_back(make_pair(1, i)); for (int i = 0; i < s3.size(); ++i) if (s3[i] == 'x') piece_parts.back().push_back(make_pair(2, i)); for (int i = 0; i < s4.size(); ++i) if (s4[i] == 'x') piece_parts.back().push_back(make_pair(3, i)); piece_masks.resize(piece_masks.size() + 1); int x_max = 0, y_max = 0; for (int i = 0; i < piece_parts.back().size(); ++i) { x_max = max(x_max, piece_parts.back()[i].first ); y_max = max(y_max, piece_parts.back()[i].second); } piece_masks.back().resize(x_max + 5, vector<bool>(y_max + 5)); for (int i = 0; i < piece_parts.back().size(); ++i) for (int d = 0; d <= 4; ++d) { piece_masks.back()[kDi[d] + 2 + piece_parts.back()[i].first][kDj[d] + 2 + piece_parts.back()[i].second] = true; } } void LearnPiece(const string& s1, const string& s2, const string& s3) { LearnPiece(s1, s2, s3, ""); } void LearnPiece(const string& s1, const string& s2) { LearnPiece(s1, s2, ""); } void LearnPiece(const string& s1) { LearnPiece(s1, ""); } void LearnPieces() { // 0 LearnPiece("xx"); // 1 LearnPiece("x", "x"); // 2 LearnPiece("xxx"); // 3 LearnPiece("x", "x", "x"); // 4 LearnPiece("xx", "x "); // 5 LearnPiece("xx", " x"); // 6 LearnPiece("x ", "xx"); // 7 LearnPiece(" x", "xx"); // 8 LearnPiece("xxxx"); // 9 LearnPiece("x", "x", "x", "x"); // 10 LearnPiece("xxx", " x"); // 11 LearnPiece("x ", "xxx"); // 12 LearnPiece("xx", "x ", "x "); // 13 LearnPiece(" x", " x", "xx"); // 14 LearnPiece(" x", "xxx"); // 15 LearnPiece("xxx", "x "); // 16 LearnPiece("xx", " x", " x"); // 17 LearnPiece("x ", "x ", "xx"); // 18 LearnPiece("xxx", " x "); // 19 LearnPiece(" x ", "xxx"); // 20 LearnPiece(" x", "xx", " x"); // 21 LearnPiece("x ", "xx", "x "); // 22 LearnPiece("xx ", " xx"); // 23 LearnPiece(" x", "xx", "x "); LearnPiece("x ", "xx", " x"); LearnPiece("xx", "xx"); } int GetPieceCount() { return piece_parts.size(); } int GetPieceSize(const int k) { return piece_parts[k].size(); } vector<vector<int> > dist; bool PieceFits(const int x, const int y, const int k) { bool one_found = false, two_found = false; for (int i = 0; i < piece_parts[k].size(); ++i) { const int ii = x + piece_parts[k][i].first; const int jj = y + piece_parts[k][i].second; if (dist[ii][jj] == 0) return false; if (dist[ii][jj] == 3) return false; if (dist[ii][jj] == 1) one_found = true; if (dist[ii][jj] == 2) two_found = true; } return one_found && two_found; } vector<vector<long long> > ways; void ComputePieceDeltas(int x, int y, const int k, long long* C1, long long* W2) { x -= 2; y -= 2; *C1 = 0; *W2 = 0; for (int i = 0; i < piece_masks[k].size(); ++i) for (int j = 0; j < piece_masks[k][i].size(); ++j) if (piece_masks[k][i][j]) { const int ii = x + i; const int jj = y + j; // cerr << "masked " << ii << " " << jj << endl; if (dist[ii][jj] == 2) *W2 += ways[ii][jj]; else if (dist[ii][jj] == 1) { ++*C1; for (int d = 0; d < 4; ++d) if (!piece_masks[k][i + kDi[d]][j + kDj[d]]) { const int iii = ii + kDi[d]; const int jjj = jj + kDj[d]; if (dist[iii][jjj] == 2) ++*W2; } } // cerr << *C1 << ' ' << *W2 << endl; } } int n; vector<long long> answer; void ComputeStats() { long long C1 = 0, W2 = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if (dist[i][j] == 1) ++C1; if (dist[i][j] == 2) W2 += ways[i][j]; } answer.resize(6); for (int i = 0; i <= 5; ++i) AddTo(Choose(C1, i), &answer[i]); // cerr << "Fitting pieces" << endl; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) for (int k = 0; k < GetPieceCount(); ++k) if (PieceFits(i, j, k)) { // cerr << k << " fits at " << i << ' ' << j << endl; const int size = GetPieceSize(k); // cerr << "piece size " << size << endl; AddTo(1, &answer[size]); if (size < 4) { long long C1_delta, W2_delta; ComputePieceDeltas(i, j, k, &C1_delta, &W2_delta); // cerr << "deltas " << C1_delta << ' ' << W2_delta << endl; AddTo( C1 - C1_delta , &answer[size + 1]); AddTo(Choose(C1 - C1_delta, 2), &answer[size + 2]); AddTo( W2 - W2_delta , &answer[size + 2]); } // cerr << "answers"; // for (int i = 0; i < 5; ++i) cerr << ' ' << answer[i]; // cerr << endl; } } void Spread(const int s) { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (dist[i][j] == s) for (int d = 0; d < 4; ++d) { const int ii = i + kDi[d]; const int jj = j + kDj[d]; if (dist[ii][jj] == -1) dist[ii][jj] = s + 1; if (dist[ii][jj] == s + 1) ways[ii][jj] += ways[i][j]; } } int main() { LearnPieces(); int k; scanf("%d%d", &n, &k); dist.resize(n + 5, vector<int>(n + 5, 3)); ways.resize(n + 5, vector<long long>(n + 5)); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { char c; do { scanf("%c", &c); } while (c != '#' && c != '.'); if (c == '#') { dist[i][j] = 0; } else { dist[i][j] = -1; } } Spread(0); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (dist[i][j] == 1) ways[i][j] = 1; Spread(1); ComputeStats(); printf("%lld\n", answer[k]); return 0; } |