#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, k; ll p, d, t, c; const ll m = 1e9 + 7; int T[3001][3001]; int d_x[] = {-1, 1, 0, 0}; int d_y[] = {0, 0, 1, -1}; vector<pair<int,int> > wolne; ll counter_p(ll ctr) { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'w') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'k') { T[i][j] = 'p'; } } } } } if(T[i][j] == ctr) res++; } } return res; } ll counter_d(ll ctr) { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'w') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'p') { T[i][j] = 'd'; } } } } } if(T[i][j] == ctr) res++; } } return res; } ll counter_t(ll ctr) { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'w') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { T[i][j] = 't'; } } } } } if(T[i][j] == 'd') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { T[i][j] = 't'+1; } } } } } if(T[i][j] == ctr) res++; } } return res; } ll sumaTakichZePiD() { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'p') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { res++; res%=m; } } } } } } } return res%m; } ll sumaIloczynowD() { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'p') { ll tmp = 0; for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { tmp++; } } } } res += ((((tmp%m)*((tmp-1)%m))%m)*500000004)%m; } } } return res%m; } ll sumaTakichAAB() { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'd' || T[i][j] == 't'+1) { ll tmp = 0; for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'p') { tmp++; } } } } res += ((((tmp%m)*((tmp-1)%m))%m)*500000004)%m; res += ((tmp%m)*((m+p-tmp)%m))%m; } } } return res%m; } ll ileTychT() { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'p') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { for(int h = 0; h < 4; h++) { if(1 <= i + d_x[q] + d_x[h] && i + d_x[q] + d_x[h] <= n) { if(1 <= j + d_y[q] + d_y[h] && j + d_y[q] + d_y[h] <= n) { if(T[i + d_x[q] + d_x[h]][j + d_y[q] + d_y[h]] == 't' || T[i + d_x[q] + d_x[h]][j + d_y[q] + d_y[h]] == 't'+1) { res++; res%=m; } } } } } } } } } } } return res%m; } ll brutDlaK4(){ ll res = 0; for(int a1 = 0; a1 < (int)wolne.size(); a1++) { for(int b1 = a1+1; b1 < (int)wolne.size(); b1++) { for(int c1 = b1+1; c1 < (int)wolne.size(); c1++) { for(int d1 = c1+1; d1 < (int)wolne.size(); d1++) { int tab[] = {a1,b1,c1,d1}; bool ok = false; do { pair<int,int> a = wolne[tab[0]]; pair<int,int> b = wolne[tab[1]]; pair<int,int> c = wolne[tab[2]]; pair<int,int> d = wolne[tab[3]]; ll saOK = 0; for(int q = 0; q < 4; q++) { if(1 <= a.first + d_x[q] && a.first + d_x[q] <= n) { if(1 <= a.second + d_y[q] && a.second + d_y[q] <= n) { if(T[a.first + d_x[q]][a.second + d_y[q]] == 'k') { T[a.first][a.second]='k'; saOK++; break; } } } } for(int q = 0; q < 4; q++) { if(1 <= b.first + d_x[q] && b.first + d_x[q] <= n) { if(1 <= b.second + d_y[q] && b.second + d_y[q] <= n) { if(T[b.first + d_x[q]][b.second + d_y[q]] == 'k') { T[b.first][b.second]='k'; saOK++; break; } } } } for(int q = 0; q < 4; q++) { if(1 <= c.first + d_x[q] && c.first + d_x[q] <= n) { if(1 <= c.second + d_y[q] && c.second + d_y[q] <= n) { if(T[c.first + d_x[q]][c.second + d_y[q]] == 'k') { T[c.first][c.second]='k'; saOK++; break; } } } } for(int q = 0; q < 4; q++) { if(1 <= d.first + d_x[q] && d.first + d_x[q] <= n) { if(1 <= d.second + d_y[q] && d.second + d_y[q] <= n) { if(T[d.first + d_x[q]][d.second + d_y[q]] == 'k') { T[d.first][d.second]='k'; saOK++; break; } } } } if(saOK == 4) { ok = true; res++; res%=m; } T[a.first][a.second] = T[b.first][b.second] = T[c.first][c.second] = T[d.first][d.second] = 'w'; } while(std::next_permutation(tab,tab+4) && ok == false); } } } } return res; } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { char c; cin >> c; if(c == '.') T[i][j] = 'w'; else T[i][j] = 'k'; } } if(k == 4) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'w') wolne.push_back(make_pair(i, j)); } } cout << brutDlaK4() << endl; return 0; } p = counter_p('p'); d = counter_d('d'); t = counter_t('t'); if(k == 1) { cout << p % m << endl; } else if(k == 2) { ll sameP = ((((p%m)*((m+p-1)%m))%m)*500000004)%m; cout << (sameP%m + sumaTakichZePiD()%m) % m << endl; } else if(k == 3) { ll sameP = (((((p%m)*((m+p-1)%m))%m*((m+p-2)%m))%m)*166666668)%m; cout << (((sameP%m + sumaTakichAAB()%m)%m + sumaIloczynowD()%m)%m + ileTychT()%m)%m << 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int n, k; ll p, d, t, c; const ll m = 1e9 + 7; int T[3001][3001]; int d_x[] = {-1, 1, 0, 0}; int d_y[] = {0, 0, 1, -1}; vector<pair<int,int> > wolne; ll counter_p(ll ctr) { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'w') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'k') { T[i][j] = 'p'; } } } } } if(T[i][j] == ctr) res++; } } return res; } ll counter_d(ll ctr) { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'w') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'p') { T[i][j] = 'd'; } } } } } if(T[i][j] == ctr) res++; } } return res; } ll counter_t(ll ctr) { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'w') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { T[i][j] = 't'; } } } } } if(T[i][j] == 'd') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { T[i][j] = 't'+1; } } } } } if(T[i][j] == ctr) res++; } } return res; } ll sumaTakichZePiD() { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'p') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { res++; res%=m; } } } } } } } return res%m; } ll sumaIloczynowD() { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'p') { ll tmp = 0; for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { tmp++; } } } } res += ((((tmp%m)*((tmp-1)%m))%m)*500000004)%m; } } } return res%m; } ll sumaTakichAAB() { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'd' || T[i][j] == 't'+1) { ll tmp = 0; for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'p') { tmp++; } } } } res += ((((tmp%m)*((tmp-1)%m))%m)*500000004)%m; res += ((tmp%m)*((m+p-tmp)%m))%m; } } } return res%m; } ll ileTychT() { ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'p') { for(int q = 0; q < 4; q++) { if(1 <= i + d_x[q] && i + d_x[q] <= n) { if(1 <= j + d_y[q] && j + d_y[q] <= n) { if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) { for(int h = 0; h < 4; h++) { if(1 <= i + d_x[q] + d_x[h] && i + d_x[q] + d_x[h] <= n) { if(1 <= j + d_y[q] + d_y[h] && j + d_y[q] + d_y[h] <= n) { if(T[i + d_x[q] + d_x[h]][j + d_y[q] + d_y[h]] == 't' || T[i + d_x[q] + d_x[h]][j + d_y[q] + d_y[h]] == 't'+1) { res++; res%=m; } } } } } } } } } } } return res%m; } ll brutDlaK4(){ ll res = 0; for(int a1 = 0; a1 < (int)wolne.size(); a1++) { for(int b1 = a1+1; b1 < (int)wolne.size(); b1++) { for(int c1 = b1+1; c1 < (int)wolne.size(); c1++) { for(int d1 = c1+1; d1 < (int)wolne.size(); d1++) { int tab[] = {a1,b1,c1,d1}; bool ok = false; do { pair<int,int> a = wolne[tab[0]]; pair<int,int> b = wolne[tab[1]]; pair<int,int> c = wolne[tab[2]]; pair<int,int> d = wolne[tab[3]]; ll saOK = 0; for(int q = 0; q < 4; q++) { if(1 <= a.first + d_x[q] && a.first + d_x[q] <= n) { if(1 <= a.second + d_y[q] && a.second + d_y[q] <= n) { if(T[a.first + d_x[q]][a.second + d_y[q]] == 'k') { T[a.first][a.second]='k'; saOK++; break; } } } } for(int q = 0; q < 4; q++) { if(1 <= b.first + d_x[q] && b.first + d_x[q] <= n) { if(1 <= b.second + d_y[q] && b.second + d_y[q] <= n) { if(T[b.first + d_x[q]][b.second + d_y[q]] == 'k') { T[b.first][b.second]='k'; saOK++; break; } } } } for(int q = 0; q < 4; q++) { if(1 <= c.first + d_x[q] && c.first + d_x[q] <= n) { if(1 <= c.second + d_y[q] && c.second + d_y[q] <= n) { if(T[c.first + d_x[q]][c.second + d_y[q]] == 'k') { T[c.first][c.second]='k'; saOK++; break; } } } } for(int q = 0; q < 4; q++) { if(1 <= d.first + d_x[q] && d.first + d_x[q] <= n) { if(1 <= d.second + d_y[q] && d.second + d_y[q] <= n) { if(T[d.first + d_x[q]][d.second + d_y[q]] == 'k') { T[d.first][d.second]='k'; saOK++; break; } } } } if(saOK == 4) { ok = true; res++; res%=m; } T[a.first][a.second] = T[b.first][b.second] = T[c.first][c.second] = T[d.first][d.second] = 'w'; } while(std::next_permutation(tab,tab+4) && ok == false); } } } } return res; } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { char c; cin >> c; if(c == '.') T[i][j] = 'w'; else T[i][j] = 'k'; } } if(k == 4) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(T[i][j] == 'w') wolne.push_back(make_pair(i, j)); } } cout << brutDlaK4() << endl; return 0; } p = counter_p('p'); d = counter_d('d'); t = counter_t('t'); if(k == 1) { cout << p % m << endl; } else if(k == 2) { ll sameP = ((((p%m)*((m+p-1)%m))%m)*500000004)%m; cout << (sameP%m + sumaTakichZePiD()%m) % m << endl; } else if(k == 3) { ll sameP = (((((p%m)*((m+p-1)%m))%m*((m+p-2)%m))%m)*166666668)%m; cout << (((sameP%m + sumaTakichAAB()%m)%m + sumaIloczynowD()%m)%m + ileTychT()%m)%m << endl; } return 0; } |