#include <iostream> #include <vector> using namespace std; #define T_OCC 0 #define T_POSS 1 #define T_M 2 #define T_L 3 #define T_FREE 4 typedef unsigned long long ULL; typedef long long LL; const ULL MOD = 1000000007; struct P { unsigned cols; char type; char m; char l; }; typedef vector<P> VP; typedef vector<VP> VVP; typedef vector<ULL> VULL; vector<VULL> Newt; void fillNewt(int N, int K) { Newt = vector<VULL>(N+1, VULL(K+1,0)); for (auto i = 0; i <= N; i++) { Newt[i][0] = 1; for (auto j = 1; j < K && j <= i; j++) { Newt[i][j] = (Newt[i-1][j-1] + Newt[i-1][j]) % MOD; } } } ULL newt(int n, int k) { if (n < k) return 0; return Newt[n][k]; } int main() { ios_base::sync_with_stdio(false); int N, K; cin >> N >> K; VVP v(N, VP(N, P())); for (auto i = 0; i < N; i++) { char s[N+3]; cin >> s; for (auto j = 0; j < N; j++) { v[i][j].type = s[j]=='#' ? T_OCC : T_FREE; } } auto validxy = [&N](int x, int y) { return x>=0&&x<N&&y>=0&&y<N; }; auto applyNeighbours = [&v,&validxy](int x, int y, void (*fun)(P &p)) { if (validxy(x-1,y)) fun(v[x-1][y]); if (validxy(x+1,y)) fun(v[x+1][y]); if (validxy(x,y-1)) fun(v[x][y-1]); if (validxy(x,y+1)) fun(v[x][y+1]); }; auto setPossIfFree = [](P &p) { if (p.type == T_FREE) p.type = T_POSS; }; // find first possibilities for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (v[i][j].type == T_OCC) applyNeighbours(i,j, setPossIfFree); } } // find ms ULL n = 0; static int m = 0; auto setMsIfFree = [](P &p) { if (p.type == T_FREE) { p.type = T_M; p.cols = 1; m++; } else if (p.type == T_M) p.cols++; }; for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (v[i][j].type == T_POSS) { n++; applyNeighbours(i,j, setMsIfFree); } } } //find ls auto setLsIfFree = [](P &p) { if (p.type == T_FREE) p.type = T_L; }; for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (v[i][j].type == T_M) applyNeighbours(i,j, setLsIfFree); } } vector<int> poss; poss.reserve(n); vector<int> ms; ms.reserve(m); for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (v[i][j].type == T_POSS) { poss.push_back(i*N+j); } else if (v[i][j].type == T_M) { ms.push_back(i*N+j); } } } fillNewt(n, K+1); if (K == 1) { cout << newt(n, K) << endl; return 0; } auto checkType = [&v, &validxy](int x, int y, char type) { return (validxy(x, y) && v[x][y].type == type) ? 1 : 0; }; // count ms ULL msum = 0; for (auto &pi : poss) { int i = pi/N; int j = pi%N; char c = 0; c += checkType(i-1, j, T_M); c += checkType(i+1, j, T_M); c += checkType(i, j-1, T_M); c += checkType(i, j+1, T_M); v[i][j].m = c; msum += c; } if (K == 2) { cout << (newt(n, K) + msum) % MOD << endl; return 0; } ULL newtksum = 0; int idx = n; for (auto it = poss.rbegin(); it != poss.rend(); ++it) { P &p = v[*it/N][*it%N]; newtksum = (newtksum + newt(p.m, K-1)) % MOD; idx--; } ULL lsum = 0; ULL cols_cnt = 0; for (auto &pi: ms) { int i = pi/N; int j = pi%N; P &p = v[i][j]; if (p.cols > 1) cols_cnt++; char c = 0; c += checkType(i-1, j, T_L); c += checkType(i+1, j, T_L); c += checkType(i, j-1, T_L); c += checkType(i, j+1, T_L); p.l = c; } for (auto &pi: poss) { int i = pi/N; int j = pi%N; P &p = v[i][j]; if (checkType(i-1, j, T_M)) p.l += v[i-1][j].l; if (checkType(i+1, j, T_M)) p.l += v[i+1][j].l; if (checkType(i, j-1, T_M)) p.l += v[i][j-1].l; if (checkType(i, j+1, T_M)) p.l += v[i][j+1].l; lsum = (lsum + p.l) % MOD; } ULL res = newt(n, K); res = (res + msum * newt(n-1, K-2) - cols_cnt * newt(n-K+2,K-3)) % MOD; res = (res + newtksum) % MOD; if (K == 3) { res = (res + lsum) % MOD; cout << res << endl; return 0; } cout << res << 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 | #include <iostream> #include <vector> using namespace std; #define T_OCC 0 #define T_POSS 1 #define T_M 2 #define T_L 3 #define T_FREE 4 typedef unsigned long long ULL; typedef long long LL; const ULL MOD = 1000000007; struct P { unsigned cols; char type; char m; char l; }; typedef vector<P> VP; typedef vector<VP> VVP; typedef vector<ULL> VULL; vector<VULL> Newt; void fillNewt(int N, int K) { Newt = vector<VULL>(N+1, VULL(K+1,0)); for (auto i = 0; i <= N; i++) { Newt[i][0] = 1; for (auto j = 1; j < K && j <= i; j++) { Newt[i][j] = (Newt[i-1][j-1] + Newt[i-1][j]) % MOD; } } } ULL newt(int n, int k) { if (n < k) return 0; return Newt[n][k]; } int main() { ios_base::sync_with_stdio(false); int N, K; cin >> N >> K; VVP v(N, VP(N, P())); for (auto i = 0; i < N; i++) { char s[N+3]; cin >> s; for (auto j = 0; j < N; j++) { v[i][j].type = s[j]=='#' ? T_OCC : T_FREE; } } auto validxy = [&N](int x, int y) { return x>=0&&x<N&&y>=0&&y<N; }; auto applyNeighbours = [&v,&validxy](int x, int y, void (*fun)(P &p)) { if (validxy(x-1,y)) fun(v[x-1][y]); if (validxy(x+1,y)) fun(v[x+1][y]); if (validxy(x,y-1)) fun(v[x][y-1]); if (validxy(x,y+1)) fun(v[x][y+1]); }; auto setPossIfFree = [](P &p) { if (p.type == T_FREE) p.type = T_POSS; }; // find first possibilities for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (v[i][j].type == T_OCC) applyNeighbours(i,j, setPossIfFree); } } // find ms ULL n = 0; static int m = 0; auto setMsIfFree = [](P &p) { if (p.type == T_FREE) { p.type = T_M; p.cols = 1; m++; } else if (p.type == T_M) p.cols++; }; for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (v[i][j].type == T_POSS) { n++; applyNeighbours(i,j, setMsIfFree); } } } //find ls auto setLsIfFree = [](P &p) { if (p.type == T_FREE) p.type = T_L; }; for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (v[i][j].type == T_M) applyNeighbours(i,j, setLsIfFree); } } vector<int> poss; poss.reserve(n); vector<int> ms; ms.reserve(m); for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (v[i][j].type == T_POSS) { poss.push_back(i*N+j); } else if (v[i][j].type == T_M) { ms.push_back(i*N+j); } } } fillNewt(n, K+1); if (K == 1) { cout << newt(n, K) << endl; return 0; } auto checkType = [&v, &validxy](int x, int y, char type) { return (validxy(x, y) && v[x][y].type == type) ? 1 : 0; }; // count ms ULL msum = 0; for (auto &pi : poss) { int i = pi/N; int j = pi%N; char c = 0; c += checkType(i-1, j, T_M); c += checkType(i+1, j, T_M); c += checkType(i, j-1, T_M); c += checkType(i, j+1, T_M); v[i][j].m = c; msum += c; } if (K == 2) { cout << (newt(n, K) + msum) % MOD << endl; return 0; } ULL newtksum = 0; int idx = n; for (auto it = poss.rbegin(); it != poss.rend(); ++it) { P &p = v[*it/N][*it%N]; newtksum = (newtksum + newt(p.m, K-1)) % MOD; idx--; } ULL lsum = 0; ULL cols_cnt = 0; for (auto &pi: ms) { int i = pi/N; int j = pi%N; P &p = v[i][j]; if (p.cols > 1) cols_cnt++; char c = 0; c += checkType(i-1, j, T_L); c += checkType(i+1, j, T_L); c += checkType(i, j-1, T_L); c += checkType(i, j+1, T_L); p.l = c; } for (auto &pi: poss) { int i = pi/N; int j = pi%N; P &p = v[i][j]; if (checkType(i-1, j, T_M)) p.l += v[i-1][j].l; if (checkType(i+1, j, T_M)) p.l += v[i+1][j].l; if (checkType(i, j-1, T_M)) p.l += v[i][j-1].l; if (checkType(i, j+1, T_M)) p.l += v[i][j+1].l; lsum = (lsum + p.l) % MOD; } ULL res = newt(n, K); res = (res + msum * newt(n-1, K-2) - cols_cnt * newt(n-K+2,K-3)) % MOD; res = (res + newtksum) % MOD; if (K == 3) { res = (res + lsum) % MOD; cout << res << endl; return 0; } cout << res << endl; return 0; } |