#include <algorithm> #include <iostream> #include <vector> #include <cassert> #include <queue> #include <set> constexpr int MAX_SIZE = 3024; constexpr int64_t MOD = 1e9+7; struct Point { int x, y; }; struct TrieNode { int chr[2]; TrieNode() { chr[0] = chr[1] = -1; } }; int width, moves; char board[MAX_SIZE][MAX_SIZE]; int64_t counts[4], ways[4]; int64_t waysFor[MAX_SIZE][MAX_SIZE]; int64_t toAdj[MAX_SIZE][MAX_SIZE]; int64_t toNextChooseTwo[4], toPrevWithTwo[4]; std::vector<TrieNode> nodes; int64_t bruteRet; int64_t modInv(int64_t a, int64_t b) { int64_t u = 1, v = 0, x = 0, y = 1; int64_t m = b; while (a > 0) { int64_t q = b / a, r = b % a; int64_t m = x - u*q, n = y - v*q; b = a; a = r; x = u; y = v; u = m; v = n; } return (b == 1 ? (x < 0 ? x+m : x) : 0); } template<typename T> void eachNeigh(int x, int y, T func) { if (x > 0) { func(x-1, y); } if (x+1 < width) { func(x+1, y); } if (y > 0) { func(x, y-1); } if (y+1 < width) { func(x, y+1); } } void insertOpt() { int cur = 0; bool add = false; for (int y = 0; y < width; y++) { for (int x = 0; x < width; x++) { int dir = (board[x][y] == 0 ? 1 : 0); if (nodes[cur].chr[dir] < 0) { nodes[cur].chr[dir] = nodes.size(); nodes.emplace_back(); add = true; } cur = nodes[cur].chr[dir]; } } bruteRet += (add ? 1 : 0); } void bruteSolve(int moves) { for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { if (board[x][y] == 0) { continue; } bool ok = false; if (x > 0) { ok |= (board[x-1][y] == 0); } if (x+1 < width) { ok |= (board[x+1][y] == 0); } if (y > 0) { ok |= (board[x][y-1] == 0); } if (y+1 < width) { ok |= (board[x][y+1] == 0); } if (ok) { board[x][y] = 0; if (moves == 1) { insertOpt(); } else { bruteSolve(moves-1); } board[x][y] = 100; } } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cin >> width >> moves; for (int y = 0; y < width; y++) { std::string tmp; std::cin >> tmp; for (int x = 0; x < width; x++) { board[y][x] = (tmp[x] == '#' ? 0 : 100); } } if (moves >= 4) { nodes.emplace_back(); bruteSolve(moves); std::cout << bruteRet % MOD << std::endl; return 0; } std::queue<Point> que; for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { if (board[x][y] == 0) { que.push({ x, y }); } } } // Compute while (!que.empty()) { Point elem = que.front(); que.pop(); int dist = board[elem.x][elem.y]; int toPrev = 0, toNext = 0; eachNeigh(elem.x, elem.y, [&](int x, int y) { if (board[x][y] >= dist+1) { if (dist == 0) { waysFor[x][y] = 1; } else { waysFor[x][y] = (waysFor[x][y] + waysFor[elem.x][elem.y]) % MOD; } ways[dist+1] = (ways[dist+1] + waysFor[elem.x][elem.y]) % MOD; toNext++; } if (board[x][y] == dist-1) { toPrev++; } if (board[x][y] == dist) { toAdj[elem.x][elem.y]++; } if (board[x][y] > dist+1) { board[x][y] = dist+1; if (dist+1 <= moves) { que.push({ x, y }); } } }); toNextChooseTwo[dist] = (toNextChooseTwo[dist] + toNext*(toNext-1)/2) % MOD; toPrevWithTwo[dist] = (toPrevWithTwo[dist] + toPrev*(counts[dist-1] - toPrev) + toPrev*(toPrev-1)/2) % MOD; counts[dist]++; } // Sum int64_t ret = 0; if (moves == 1) { ret = counts[1] % MOD; } else if (moves == 2) { ret = (counts[1]*(counts[1]-1)/2 + ways[2]) % MOD; } else if (moves == 3) { ret = (((((counts[1]*(counts[1]-1)) % MOD) * (counts[1]-2)) % MOD) * modInv(6, MOD)) % MOD; ret = (ret + ways[3] + toNextChooseTwo[1] + toPrevWithTwo[2]) % MOD; for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { if (board[x][y] != 1) { continue; } eachNeigh(x, y, [&](int x2, int y2) { if (board[x2][y2] == 2) { ret += toAdj[x2][y2]; } }); } } ret = ret % MOD; } std::cout << ret << std::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 | #include <algorithm> #include <iostream> #include <vector> #include <cassert> #include <queue> #include <set> constexpr int MAX_SIZE = 3024; constexpr int64_t MOD = 1e9+7; struct Point { int x, y; }; struct TrieNode { int chr[2]; TrieNode() { chr[0] = chr[1] = -1; } }; int width, moves; char board[MAX_SIZE][MAX_SIZE]; int64_t counts[4], ways[4]; int64_t waysFor[MAX_SIZE][MAX_SIZE]; int64_t toAdj[MAX_SIZE][MAX_SIZE]; int64_t toNextChooseTwo[4], toPrevWithTwo[4]; std::vector<TrieNode> nodes; int64_t bruteRet; int64_t modInv(int64_t a, int64_t b) { int64_t u = 1, v = 0, x = 0, y = 1; int64_t m = b; while (a > 0) { int64_t q = b / a, r = b % a; int64_t m = x - u*q, n = y - v*q; b = a; a = r; x = u; y = v; u = m; v = n; } return (b == 1 ? (x < 0 ? x+m : x) : 0); } template<typename T> void eachNeigh(int x, int y, T func) { if (x > 0) { func(x-1, y); } if (x+1 < width) { func(x+1, y); } if (y > 0) { func(x, y-1); } if (y+1 < width) { func(x, y+1); } } void insertOpt() { int cur = 0; bool add = false; for (int y = 0; y < width; y++) { for (int x = 0; x < width; x++) { int dir = (board[x][y] == 0 ? 1 : 0); if (nodes[cur].chr[dir] < 0) { nodes[cur].chr[dir] = nodes.size(); nodes.emplace_back(); add = true; } cur = nodes[cur].chr[dir]; } } bruteRet += (add ? 1 : 0); } void bruteSolve(int moves) { for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { if (board[x][y] == 0) { continue; } bool ok = false; if (x > 0) { ok |= (board[x-1][y] == 0); } if (x+1 < width) { ok |= (board[x+1][y] == 0); } if (y > 0) { ok |= (board[x][y-1] == 0); } if (y+1 < width) { ok |= (board[x][y+1] == 0); } if (ok) { board[x][y] = 0; if (moves == 1) { insertOpt(); } else { bruteSolve(moves-1); } board[x][y] = 100; } } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cin >> width >> moves; for (int y = 0; y < width; y++) { std::string tmp; std::cin >> tmp; for (int x = 0; x < width; x++) { board[y][x] = (tmp[x] == '#' ? 0 : 100); } } if (moves >= 4) { nodes.emplace_back(); bruteSolve(moves); std::cout << bruteRet % MOD << std::endl; return 0; } std::queue<Point> que; for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { if (board[x][y] == 0) { que.push({ x, y }); } } } // Compute while (!que.empty()) { Point elem = que.front(); que.pop(); int dist = board[elem.x][elem.y]; int toPrev = 0, toNext = 0; eachNeigh(elem.x, elem.y, [&](int x, int y) { if (board[x][y] >= dist+1) { if (dist == 0) { waysFor[x][y] = 1; } else { waysFor[x][y] = (waysFor[x][y] + waysFor[elem.x][elem.y]) % MOD; } ways[dist+1] = (ways[dist+1] + waysFor[elem.x][elem.y]) % MOD; toNext++; } if (board[x][y] == dist-1) { toPrev++; } if (board[x][y] == dist) { toAdj[elem.x][elem.y]++; } if (board[x][y] > dist+1) { board[x][y] = dist+1; if (dist+1 <= moves) { que.push({ x, y }); } } }); toNextChooseTwo[dist] = (toNextChooseTwo[dist] + toNext*(toNext-1)/2) % MOD; toPrevWithTwo[dist] = (toPrevWithTwo[dist] + toPrev*(counts[dist-1] - toPrev) + toPrev*(toPrev-1)/2) % MOD; counts[dist]++; } // Sum int64_t ret = 0; if (moves == 1) { ret = counts[1] % MOD; } else if (moves == 2) { ret = (counts[1]*(counts[1]-1)/2 + ways[2]) % MOD; } else if (moves == 3) { ret = (((((counts[1]*(counts[1]-1)) % MOD) * (counts[1]-2)) % MOD) * modInv(6, MOD)) % MOD; ret = (ret + ways[3] + toNextChooseTwo[1] + toPrevWithTwo[2]) % MOD; for (int x = 0; x < width; x++) { for (int y = 0; y < width; y++) { if (board[x][y] != 1) { continue; } eachNeigh(x, y, [&](int x2, int y2) { if (board[x2][y2] == 2) { ret += toAdj[x2][y2]; } }); } } ret = ret % MOD; } std::cout << ret << std::endl; return 0; } |