#include <cstdio> #include <algorithm> #include <vector> #include <cmath> #include <iostream> #include <ctime> using namespace std; struct tuple4 { int a, b, c, d; tuple4(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {} }; const int MAX = 7; // Max windows size const int SET = 9; // number of neighbours representing initially occupied field const int MODULO = 1000000007; void printTime() { static int start = clock(); static int i = 0; clock_t end = clock(); if (i > 0) { double elapsed_secs = double(end - start) / CLOCKS_PER_SEC; start = end; cout << i << " Time: " << elapsed_secs << endl; } i++; } long long combinations(const int posibleChoicesNumber, const int choose) { unsigned long long mul = 1; int div = 1; if (choose > posibleChoicesNumber) { return 0; } for(int i = 0; i < choose; i++) { mul = mul * (posibleChoicesNumber -i); div = div * (i+1); } //cout << mul << ":" << div << endl; return (mul/div)%MODULO; } int readField(vector< vector<bool> > &field, const int n) { int lastNeigh[2][3000]; char x; int fieldsWithNeighbours = 0; for (int i = 0; i < n; i++) { int lastRowIdx = 0; if(i>0) { lastRowIdx = (i-1) % 2; } int rowIdx = i % 2; for (int j = 0; j < n; j++) { int neigh = 0; cin >> x; if ('#' == x) { neigh = SET; //cout << '#'; field[i][j] = true; if (i>0) { if (lastNeigh[lastRowIdx][j] == 0) { fieldsWithNeighbours++; } if (lastNeigh[lastRowIdx][j] != SET) { lastNeigh[lastRowIdx][j] += 1; } } if (j>0) { if (lastNeigh[rowIdx][j-1] == 0) { fieldsWithNeighbours++; } if (lastNeigh[rowIdx][j-1] != SET) { lastNeigh[rowIdx][j-1] += 1; } } } else { //cout << '.'; field[i][j] = false; } if (neigh != SET && i>0 && field[i-1][j]) { neigh += 1; } if (neigh != SET && j>0 && field[i][j-1]) { neigh += 1; } if (neigh != SET && neigh > 0 && !field[i][j]) { fieldsWithNeighbours++; } lastNeigh[rowIdx][j] = neigh; } //cout << endl; } return fieldsWithNeighbours; } int sumDiagonals(int diagonalSum[5], int diagonal, int k) { if (diagonal == 2 && k >= 2) { return diagonalSum[2]; } else if (diagonal == 3 && k >= 3) { return max(diagonalSum[2] - 1 + diagonalSum[3], 0); } else if (diagonal == 4 && k >= 4) { return max(diagonalSum[2] -1 + diagonalSum[3] - 1 + diagonalSum[4], 0); } else { return 0; } } int getSumOfNeighbours(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int row, const int column) { int sum = 0; if (row < 0 || row >= n || column < 0 || column >= n || lastNeigh[(row) % (2*k+1)][column] != 0 || field[row][column] || k < 2) { return 0; } if (row - 1 >= 0 && lastNeigh[(row-1) % (2*k+1)][column] != SET && lastNeigh[(row-1) % (2*k+1)][column] != 0) { sum += 1; //cout << "(" << row-1 << "," << column << "#" << sum << ")"; } if (row + 1 < n && lastNeigh[(row+1) % (2*k+1)][column] != SET && lastNeigh[(row+1) % (2*k+1)][column] != 0) { sum += 1; //cout << "(" << row+1 << "," << column << "#" << sum << ")"; } if (column - 1 >= 0 && lastNeigh[(row) % (2*k+1)][column-1] != SET && lastNeigh[(row) % (2*k+1)][column-1] != 0) { sum += 1; //cout << "(" << row << "," << column-1 << "#" << sum << ")"; } if (column + 1 < n && lastNeigh[(row) % (2*k+1)][column+1] != SET && lastNeigh[(row) % (2*k+1)][column+1] != 0) { sum += 1; //cout << "(" << row << "," << column+1 << "#" << sum << ")"; } return sum; } int getSumOfZeroNeighbours(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int row, const int column) { int sum = 0; if (row < 0 || row >= n || column < 0 || column >= n || field[row][column]) { return 0; } if (row - 1 >= 0 && lastNeigh[(row-1) % (2*k+1)][column] == 0 && !field[row-1][column]) { sum += 1; //cout << "(" << row-1 << "," << column << "#" << sum << ")"; } if (row + 1 < n && lastNeigh[(row+1) % (2*k+1)][column] == 0 && !field[row+1][column]) { sum += 1; //cout << "(" << row+1 << "," << column << "#" << sum << ")"; } if (column - 1 >= 0 && lastNeigh[(row) % (2*k+1)][column-1] == 0 && !field[row][column-1]) { sum += 1; //cout << "(" << row << "," << column-1 << "#" << sum << ")"; } if (column + 1 < n && lastNeigh[(row) % (2*k+1)][column+1] == 0 && !field[row][column+1]) { sum += 1; //cout << "(" << row << "," << column+1 << "#" << sum << ")"; } return sum; } int getSumOfNeighZeroNeighbours(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int row, const int column) { int sum = 0; if (row < 0 || row >= n || column < 0 || column >= n) { return 0; } if (row - 1 >= 0 && lastNeigh[(row-1) % (2*k+1)][column] == 0 && !field[row-1][column]) { sum += getSumOfZeroNeighbours(field, lastNeigh, n, k, row-1, column); //cout << "(" << row-1 << "," << column << "#" << sum << ")"; } if (row + 1 < n && lastNeigh[(row+1) % (2*k+1)][column] == 0 && !field[row+1][column]) { sum += getSumOfZeroNeighbours(field, lastNeigh, n, k, row+1, column); //cout << "(" << row+1 << "," << column << "#" << sum << ")"; } if (column - 1 >= 0 && lastNeigh[(row) % (2*k+1)][column-1] == 0 && !field[row][column-1]) { sum += getSumOfZeroNeighbours(field, lastNeigh, n, k, row, column-1); //cout << "(" << row << "," << column-1 << "#" << sum << ")"; } if (column + 1 < n && lastNeigh[(row) % (2*k+1)][column+1] == 0 && !field[row][column+1]) { sum += getSumOfZeroNeighbours(field, lastNeigh, n, k, row, column+1); //cout << "(" << row << "," << column+1 << "#" << sum << ")"; } return sum; } long long getSum(int diagonalSum[5], const int k, const int fieldsWithNeighbours) { int additional = (k >= 3) ? max(diagonalSum[1]-diagonalSum[2], 0) : 0; //cout << "A:" << additional << "D:" << diagonalSum[1] << endl; //long long sum = diagonalSum[1]*(combinations(fieldsWithNeighbours-diagonalSum[1],k-2) + sumDiagonals(diagonalSum, 3, k)*combinations(fieldsWithNeighbours-diagonalSum[1],k-3) + sumDiagonals(diagonalSum, 3, k)*sumDiagonals(diagonalSum, 4, k)) + additional; long long sum = diagonalSum[1]*1; if (additional > 0) { //cout << "#" << diagonalSum[1] << ":" << diagonalSum[2] << ":" << diagonalSum[3] << ":" << diagonalSum[4] << fieldsWithNeighbours << " - " << k << "#"; } if (k == 3) { sum = sum * (combinations(fieldsWithNeighbours-diagonalSum[1],1) + additional + sumDiagonals(diagonalSum, 3, k)); } if (k == 4) { sum = sum * (combinations(fieldsWithNeighbours-diagonalSum[1], 2) + sumDiagonals(diagonalSum, 3, k)*combinations(fieldsWithNeighbours-1,1) + sumDiagonals(diagonalSum, 3, k)*sumDiagonals(diagonalSum, 4, k)) + max(diagonalSum[1], 0); } if (sum > 0) { //cout << combinations(fieldsWithNeighbours-diagonalSum[1],k-2) << " " << sumDiagonals(diagonalSum, 3, k)*combinations(fieldsWithNeighbours-diagonalSum[1],k-3) << " " << sumDiagonals(diagonalSum, 3, k)*sumDiagonals(diagonalSum, 4, k) << endl; } return sum; } long long checkCellAdditionalSolutions(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int fieldsWithNeighbours, const int row, const int column) { long long additionalSol = 0; int additionalPathToAlreadyAnalyzed = 0; int diagonalSum[5]; diagonalSum[1] = 0; // first diagonalSum[2] = 0; // second diagonalSum[3] = 0; // third diagonalSum[4] = 0; //fourth int initRowIdx = row % (2*k+1); //cout << "(" << row << "," << column << ")" << " # " << lastNeigh[initRowIdx][column]; if (lastNeigh[initRowIdx][column] == 0 || lastNeigh[initRowIdx][column] == SET) { //cout << endl; return 0; } int top = (k>=2) ? getSumOfNeighbours(field, lastNeigh, n, k, row-1, column) : 0; int bottom = (k>=2) ? getSumOfNeighbours(field, lastNeigh, n, k, row+1, column) : 0; int left = (k>=2) ? getSumOfNeighbours(field, lastNeigh, n, k, row, column-1) : 0; int right = (k>=2) ? getSumOfNeighbours(field, lastNeigh, n, k, row, column+1) : 0; int topZero = (k>=3) ? getSumOfZeroNeighbours(field, lastNeigh, n, k, row-1, column) : 0; int bottomZero = (k>=3) ? getSumOfZeroNeighbours(field, lastNeigh, n, k, row+1, column) : 0; int leftZero = (k>=3) ? getSumOfZeroNeighbours(field, lastNeigh, n, k, row, column-1) : 0; int rightZero = (k>=3) ? getSumOfZeroNeighbours(field, lastNeigh, n, k, row, column+1) : 0; int topNeighZero = (k>=4) ? getSumOfNeighZeroNeighbours(field, lastNeigh, n, k, row-1, column) : 0; int bottomNeighZero = (k>=4) ? getSumOfNeighZeroNeighbours(field, lastNeigh, n, k, row+1, column) : 0; int leftNeighZero = (k>=4) ? getSumOfNeighZeroNeighbours(field, lastNeigh, n, k, row, column-1) : 0; int rightNeighZero = (k>=4) ? getSumOfNeighZeroNeighbours(field, lastNeigh, n, k, row, column+1) : 0; for (int i = max(row-k, 0); i <= (row+k) && i < n; i++) { int rowIdx = i % (2*k+1); for (int j = max(column-k, 0); j <= (column+k) && j < n; j++) { int diagonal = abs(i-row)+abs(j-column)+1; if (diagonal >= 2 && diagonal <= k && lastNeigh[rowIdx][j] == 0) { if (diagonal == 2) { if (!field[i][j]) { field[i][j] = true; diagonalSum[diagonal] += 1; } else { additionalPathToAlreadyAnalyzed += 1; } } else if (diagonal > 2) { if (!field[i][j]) { diagonalSum[diagonal] += 1; } } } } } //cout << " " << top << ":" << bottom << ":" << left << ":" << right << endl; /*for(int i = 2; i <= k; i++) { if (i == 2) { additionalSol = additionalSol*(sumDiagonals(diagonalSum, 3)); } else if (i == 3) { additionalSol = additionalSol*(sumDiagonals(diagonalSum, 3) + fieldsWithNeighbours-1); } else if (i == 4) { additionalSol = additionalSol*(sumDiagonals(diagonalSum, 4) + fieldsWithNeighbours-1); } }*/ diagonalSum[1] = top; diagonalSum[3] = topZero; diagonalSum[4] = topNeighZero; int topSum = getSum(diagonalSum, k, fieldsWithNeighbours); diagonalSum[1] = bottom; diagonalSum[3] = bottomZero; diagonalSum[4] = bottomNeighZero; int bottomSum = getSum(diagonalSum, k, fieldsWithNeighbours); diagonalSum[1] = left; diagonalSum[3] = leftZero; diagonalSum[4] = leftNeighZero; int leftSum = getSum(diagonalSum, k, fieldsWithNeighbours); diagonalSum[1] = right; diagonalSum[3] = rightZero; diagonalSum[4] = rightNeighZero; int rightSum = getSum(diagonalSum, k, fieldsWithNeighbours); //cout << " " << topSum << ":" << bottomSum << ":" << leftSum << ":" << rightSum << endl; //additionalSol = additionalSol + additionalPathToAlreadyAnalyzed; additionalSol = topSum + bottomSum + leftSum + rightSum;// + additionalPathToAlreadyAnalyzed; //cout << " -> " << additionalSol << " --> " << additionalPathToAlreadyAnalyzed << endl; return additionalSol%MODULO; } long long checkRowAdditionalSolutions(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int fieldsWithNeighbours, const int row) { long long additionalSol = 0; for (int i = 0; i < n; i++) { additionalSol += checkCellAdditionalSolutions(field, lastNeigh, n, k, fieldsWithNeighbours, row, i); } //cout << "ROW: " << row << " " << additionalSol << endl; return additionalSol%MODULO; } long long checkAdditionalSolutions(vector< vector<bool> > &field, const int n, const int k, const int fieldsWithNeighbours) { long long additionalSol = 0; vector< vector<int> > lastNeigh; lastNeigh.reserve(2*k+1); for (int i = 0; i < 2*k+1; i++) { vector<int> row; row.reserve(n); for (int j = 0; j < n; j++) { row.push_back(0); } lastNeigh.push_back(row); } char x; for (int i = 0; i < n; i++) { int lastRowIdx = 0; if(i>0) { lastRowIdx = (i-1) % (2*k+1); } int rowIdx = i % (2*k+1); for (int j = 0; j < n; j++) { int neigh = 0; if (field[i][j]) { neigh = SET; if (i>0 && lastNeigh[lastRowIdx][j] != SET) { lastNeigh[lastRowIdx][j] += 1; } if (j>0 && lastNeigh[lastRowIdx][j] != SET) { lastNeigh[rowIdx][j-1] += 1; } } if (neigh != SET && i>0 && field[i-1][j]) { neigh += 1; } if (neigh != SET && j>0 && field[i][j-1]) { neigh += 1; } lastNeigh[rowIdx][j] = neigh; } if (i > k) { additionalSol += checkRowAdditionalSolutions(field, lastNeigh, n, k, fieldsWithNeighbours, i-k-1); } } for (int i = max(n-k-1, 0); i < n; i++) { additionalSol += checkRowAdditionalSolutions(field, lastNeigh, n, k, fieldsWithNeighbours, i); } return additionalSol%MODULO; } int main( ) { int n,k; scanf("%d %d", &n, &k); //cout << n << ":" << k << endl; int size = 3000; vector< vector<bool> > field; field.reserve(size); for (int i = 0; i < size; i++) { vector<bool> row; row.reserve(size); field.push_back(row); } int fieldsWithNeighbours = readField(field, n); long long additionalSolutions = checkAdditionalSolutions(field, n, k, fieldsWithNeighbours); //cout << "Fields with neighbours: " << fieldsWithNeighbours << endl; //clock_t begin = clock(); //printTime(); long long result = (additionalSolutions + combinations(fieldsWithNeighbours, k))%MODULO; cout << result << endl; //printTime(); //clock_t end = clock(); //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; //cout << "Time: " << elapsed_secs << endl; 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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | #include <cstdio> #include <algorithm> #include <vector> #include <cmath> #include <iostream> #include <ctime> using namespace std; struct tuple4 { int a, b, c, d; tuple4(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {} }; const int MAX = 7; // Max windows size const int SET = 9; // number of neighbours representing initially occupied field const int MODULO = 1000000007; void printTime() { static int start = clock(); static int i = 0; clock_t end = clock(); if (i > 0) { double elapsed_secs = double(end - start) / CLOCKS_PER_SEC; start = end; cout << i << " Time: " << elapsed_secs << endl; } i++; } long long combinations(const int posibleChoicesNumber, const int choose) { unsigned long long mul = 1; int div = 1; if (choose > posibleChoicesNumber) { return 0; } for(int i = 0; i < choose; i++) { mul = mul * (posibleChoicesNumber -i); div = div * (i+1); } //cout << mul << ":" << div << endl; return (mul/div)%MODULO; } int readField(vector< vector<bool> > &field, const int n) { int lastNeigh[2][3000]; char x; int fieldsWithNeighbours = 0; for (int i = 0; i < n; i++) { int lastRowIdx = 0; if(i>0) { lastRowIdx = (i-1) % 2; } int rowIdx = i % 2; for (int j = 0; j < n; j++) { int neigh = 0; cin >> x; if ('#' == x) { neigh = SET; //cout << '#'; field[i][j] = true; if (i>0) { if (lastNeigh[lastRowIdx][j] == 0) { fieldsWithNeighbours++; } if (lastNeigh[lastRowIdx][j] != SET) { lastNeigh[lastRowIdx][j] += 1; } } if (j>0) { if (lastNeigh[rowIdx][j-1] == 0) { fieldsWithNeighbours++; } if (lastNeigh[rowIdx][j-1] != SET) { lastNeigh[rowIdx][j-1] += 1; } } } else { //cout << '.'; field[i][j] = false; } if (neigh != SET && i>0 && field[i-1][j]) { neigh += 1; } if (neigh != SET && j>0 && field[i][j-1]) { neigh += 1; } if (neigh != SET && neigh > 0 && !field[i][j]) { fieldsWithNeighbours++; } lastNeigh[rowIdx][j] = neigh; } //cout << endl; } return fieldsWithNeighbours; } int sumDiagonals(int diagonalSum[5], int diagonal, int k) { if (diagonal == 2 && k >= 2) { return diagonalSum[2]; } else if (diagonal == 3 && k >= 3) { return max(diagonalSum[2] - 1 + diagonalSum[3], 0); } else if (diagonal == 4 && k >= 4) { return max(diagonalSum[2] -1 + diagonalSum[3] - 1 + diagonalSum[4], 0); } else { return 0; } } int getSumOfNeighbours(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int row, const int column) { int sum = 0; if (row < 0 || row >= n || column < 0 || column >= n || lastNeigh[(row) % (2*k+1)][column] != 0 || field[row][column] || k < 2) { return 0; } if (row - 1 >= 0 && lastNeigh[(row-1) % (2*k+1)][column] != SET && lastNeigh[(row-1) % (2*k+1)][column] != 0) { sum += 1; //cout << "(" << row-1 << "," << column << "#" << sum << ")"; } if (row + 1 < n && lastNeigh[(row+1) % (2*k+1)][column] != SET && lastNeigh[(row+1) % (2*k+1)][column] != 0) { sum += 1; //cout << "(" << row+1 << "," << column << "#" << sum << ")"; } if (column - 1 >= 0 && lastNeigh[(row) % (2*k+1)][column-1] != SET && lastNeigh[(row) % (2*k+1)][column-1] != 0) { sum += 1; //cout << "(" << row << "," << column-1 << "#" << sum << ")"; } if (column + 1 < n && lastNeigh[(row) % (2*k+1)][column+1] != SET && lastNeigh[(row) % (2*k+1)][column+1] != 0) { sum += 1; //cout << "(" << row << "," << column+1 << "#" << sum << ")"; } return sum; } int getSumOfZeroNeighbours(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int row, const int column) { int sum = 0; if (row < 0 || row >= n || column < 0 || column >= n || field[row][column]) { return 0; } if (row - 1 >= 0 && lastNeigh[(row-1) % (2*k+1)][column] == 0 && !field[row-1][column]) { sum += 1; //cout << "(" << row-1 << "," << column << "#" << sum << ")"; } if (row + 1 < n && lastNeigh[(row+1) % (2*k+1)][column] == 0 && !field[row+1][column]) { sum += 1; //cout << "(" << row+1 << "," << column << "#" << sum << ")"; } if (column - 1 >= 0 && lastNeigh[(row) % (2*k+1)][column-1] == 0 && !field[row][column-1]) { sum += 1; //cout << "(" << row << "," << column-1 << "#" << sum << ")"; } if (column + 1 < n && lastNeigh[(row) % (2*k+1)][column+1] == 0 && !field[row][column+1]) { sum += 1; //cout << "(" << row << "," << column+1 << "#" << sum << ")"; } return sum; } int getSumOfNeighZeroNeighbours(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int row, const int column) { int sum = 0; if (row < 0 || row >= n || column < 0 || column >= n) { return 0; } if (row - 1 >= 0 && lastNeigh[(row-1) % (2*k+1)][column] == 0 && !field[row-1][column]) { sum += getSumOfZeroNeighbours(field, lastNeigh, n, k, row-1, column); //cout << "(" << row-1 << "," << column << "#" << sum << ")"; } if (row + 1 < n && lastNeigh[(row+1) % (2*k+1)][column] == 0 && !field[row+1][column]) { sum += getSumOfZeroNeighbours(field, lastNeigh, n, k, row+1, column); //cout << "(" << row+1 << "," << column << "#" << sum << ")"; } if (column - 1 >= 0 && lastNeigh[(row) % (2*k+1)][column-1] == 0 && !field[row][column-1]) { sum += getSumOfZeroNeighbours(field, lastNeigh, n, k, row, column-1); //cout << "(" << row << "," << column-1 << "#" << sum << ")"; } if (column + 1 < n && lastNeigh[(row) % (2*k+1)][column+1] == 0 && !field[row][column+1]) { sum += getSumOfZeroNeighbours(field, lastNeigh, n, k, row, column+1); //cout << "(" << row << "," << column+1 << "#" << sum << ")"; } return sum; } long long getSum(int diagonalSum[5], const int k, const int fieldsWithNeighbours) { int additional = (k >= 3) ? max(diagonalSum[1]-diagonalSum[2], 0) : 0; //cout << "A:" << additional << "D:" << diagonalSum[1] << endl; //long long sum = diagonalSum[1]*(combinations(fieldsWithNeighbours-diagonalSum[1],k-2) + sumDiagonals(diagonalSum, 3, k)*combinations(fieldsWithNeighbours-diagonalSum[1],k-3) + sumDiagonals(diagonalSum, 3, k)*sumDiagonals(diagonalSum, 4, k)) + additional; long long sum = diagonalSum[1]*1; if (additional > 0) { //cout << "#" << diagonalSum[1] << ":" << diagonalSum[2] << ":" << diagonalSum[3] << ":" << diagonalSum[4] << fieldsWithNeighbours << " - " << k << "#"; } if (k == 3) { sum = sum * (combinations(fieldsWithNeighbours-diagonalSum[1],1) + additional + sumDiagonals(diagonalSum, 3, k)); } if (k == 4) { sum = sum * (combinations(fieldsWithNeighbours-diagonalSum[1], 2) + sumDiagonals(diagonalSum, 3, k)*combinations(fieldsWithNeighbours-1,1) + sumDiagonals(diagonalSum, 3, k)*sumDiagonals(diagonalSum, 4, k)) + max(diagonalSum[1], 0); } if (sum > 0) { //cout << combinations(fieldsWithNeighbours-diagonalSum[1],k-2) << " " << sumDiagonals(diagonalSum, 3, k)*combinations(fieldsWithNeighbours-diagonalSum[1],k-3) << " " << sumDiagonals(diagonalSum, 3, k)*sumDiagonals(diagonalSum, 4, k) << endl; } return sum; } long long checkCellAdditionalSolutions(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int fieldsWithNeighbours, const int row, const int column) { long long additionalSol = 0; int additionalPathToAlreadyAnalyzed = 0; int diagonalSum[5]; diagonalSum[1] = 0; // first diagonalSum[2] = 0; // second diagonalSum[3] = 0; // third diagonalSum[4] = 0; //fourth int initRowIdx = row % (2*k+1); //cout << "(" << row << "," << column << ")" << " # " << lastNeigh[initRowIdx][column]; if (lastNeigh[initRowIdx][column] == 0 || lastNeigh[initRowIdx][column] == SET) { //cout << endl; return 0; } int top = (k>=2) ? getSumOfNeighbours(field, lastNeigh, n, k, row-1, column) : 0; int bottom = (k>=2) ? getSumOfNeighbours(field, lastNeigh, n, k, row+1, column) : 0; int left = (k>=2) ? getSumOfNeighbours(field, lastNeigh, n, k, row, column-1) : 0; int right = (k>=2) ? getSumOfNeighbours(field, lastNeigh, n, k, row, column+1) : 0; int topZero = (k>=3) ? getSumOfZeroNeighbours(field, lastNeigh, n, k, row-1, column) : 0; int bottomZero = (k>=3) ? getSumOfZeroNeighbours(field, lastNeigh, n, k, row+1, column) : 0; int leftZero = (k>=3) ? getSumOfZeroNeighbours(field, lastNeigh, n, k, row, column-1) : 0; int rightZero = (k>=3) ? getSumOfZeroNeighbours(field, lastNeigh, n, k, row, column+1) : 0; int topNeighZero = (k>=4) ? getSumOfNeighZeroNeighbours(field, lastNeigh, n, k, row-1, column) : 0; int bottomNeighZero = (k>=4) ? getSumOfNeighZeroNeighbours(field, lastNeigh, n, k, row+1, column) : 0; int leftNeighZero = (k>=4) ? getSumOfNeighZeroNeighbours(field, lastNeigh, n, k, row, column-1) : 0; int rightNeighZero = (k>=4) ? getSumOfNeighZeroNeighbours(field, lastNeigh, n, k, row, column+1) : 0; for (int i = max(row-k, 0); i <= (row+k) && i < n; i++) { int rowIdx = i % (2*k+1); for (int j = max(column-k, 0); j <= (column+k) && j < n; j++) { int diagonal = abs(i-row)+abs(j-column)+1; if (diagonal >= 2 && diagonal <= k && lastNeigh[rowIdx][j] == 0) { if (diagonal == 2) { if (!field[i][j]) { field[i][j] = true; diagonalSum[diagonal] += 1; } else { additionalPathToAlreadyAnalyzed += 1; } } else if (diagonal > 2) { if (!field[i][j]) { diagonalSum[diagonal] += 1; } } } } } //cout << " " << top << ":" << bottom << ":" << left << ":" << right << endl; /*for(int i = 2; i <= k; i++) { if (i == 2) { additionalSol = additionalSol*(sumDiagonals(diagonalSum, 3)); } else if (i == 3) { additionalSol = additionalSol*(sumDiagonals(diagonalSum, 3) + fieldsWithNeighbours-1); } else if (i == 4) { additionalSol = additionalSol*(sumDiagonals(diagonalSum, 4) + fieldsWithNeighbours-1); } }*/ diagonalSum[1] = top; diagonalSum[3] = topZero; diagonalSum[4] = topNeighZero; int topSum = getSum(diagonalSum, k, fieldsWithNeighbours); diagonalSum[1] = bottom; diagonalSum[3] = bottomZero; diagonalSum[4] = bottomNeighZero; int bottomSum = getSum(diagonalSum, k, fieldsWithNeighbours); diagonalSum[1] = left; diagonalSum[3] = leftZero; diagonalSum[4] = leftNeighZero; int leftSum = getSum(diagonalSum, k, fieldsWithNeighbours); diagonalSum[1] = right; diagonalSum[3] = rightZero; diagonalSum[4] = rightNeighZero; int rightSum = getSum(diagonalSum, k, fieldsWithNeighbours); //cout << " " << topSum << ":" << bottomSum << ":" << leftSum << ":" << rightSum << endl; //additionalSol = additionalSol + additionalPathToAlreadyAnalyzed; additionalSol = topSum + bottomSum + leftSum + rightSum;// + additionalPathToAlreadyAnalyzed; //cout << " -> " << additionalSol << " --> " << additionalPathToAlreadyAnalyzed << endl; return additionalSol%MODULO; } long long checkRowAdditionalSolutions(vector< vector<bool> > &field, vector< vector<int> > &lastNeigh, const int n, const int k, const int fieldsWithNeighbours, const int row) { long long additionalSol = 0; for (int i = 0; i < n; i++) { additionalSol += checkCellAdditionalSolutions(field, lastNeigh, n, k, fieldsWithNeighbours, row, i); } //cout << "ROW: " << row << " " << additionalSol << endl; return additionalSol%MODULO; } long long checkAdditionalSolutions(vector< vector<bool> > &field, const int n, const int k, const int fieldsWithNeighbours) { long long additionalSol = 0; vector< vector<int> > lastNeigh; lastNeigh.reserve(2*k+1); for (int i = 0; i < 2*k+1; i++) { vector<int> row; row.reserve(n); for (int j = 0; j < n; j++) { row.push_back(0); } lastNeigh.push_back(row); } char x; for (int i = 0; i < n; i++) { int lastRowIdx = 0; if(i>0) { lastRowIdx = (i-1) % (2*k+1); } int rowIdx = i % (2*k+1); for (int j = 0; j < n; j++) { int neigh = 0; if (field[i][j]) { neigh = SET; if (i>0 && lastNeigh[lastRowIdx][j] != SET) { lastNeigh[lastRowIdx][j] += 1; } if (j>0 && lastNeigh[lastRowIdx][j] != SET) { lastNeigh[rowIdx][j-1] += 1; } } if (neigh != SET && i>0 && field[i-1][j]) { neigh += 1; } if (neigh != SET && j>0 && field[i][j-1]) { neigh += 1; } lastNeigh[rowIdx][j] = neigh; } if (i > k) { additionalSol += checkRowAdditionalSolutions(field, lastNeigh, n, k, fieldsWithNeighbours, i-k-1); } } for (int i = max(n-k-1, 0); i < n; i++) { additionalSol += checkRowAdditionalSolutions(field, lastNeigh, n, k, fieldsWithNeighbours, i); } return additionalSol%MODULO; } int main( ) { int n,k; scanf("%d %d", &n, &k); //cout << n << ":" << k << endl; int size = 3000; vector< vector<bool> > field; field.reserve(size); for (int i = 0; i < size; i++) { vector<bool> row; row.reserve(size); field.push_back(row); } int fieldsWithNeighbours = readField(field, n); long long additionalSolutions = checkAdditionalSolutions(field, n, k, fieldsWithNeighbours); //cout << "Fields with neighbours: " << fieldsWithNeighbours << endl; //clock_t begin = clock(); //printTime(); long long result = (additionalSolutions + combinations(fieldsWithNeighbours, k))%MODULO; cout << result << endl; //printTime(); //clock_t end = clock(); //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; //cout << "Time: " << elapsed_secs << endl; return 0; } |