#include <iostream>
using namespace std;
char board[3000][3000];
unsigned nChoosek( unsigned n, unsigned k )
{
if (k > n) return 0;
if (k * 2 > n) k = n-k;
if (k == 0) return 1;
int result = n;
for( int i = 2; i <= k; ++i ) {
result *= (n-i+1);
result /= i;
result &= 100000007;
}
return result;
}
int main(){
int n,k;
cin >> n >> k;
for(int y=0; y<n; y++){
for(int x=0; x<n; x++){
char c;
cin >> board[y][x];
}
}
long long ones = 0;
for(int y=0; y<n; y++){
for(int x=0; x<n; x++){
if(board[y][x]=='#'){
if(y > 0 && board[y-1][x] == '.'){
board[y-1][x] = '1';
ones++;
}
if(y < n-1 && board[y+1][x] == '.'){
board[y+1][x] = '1';
ones++;
}
if(x > 0 && board[y][x-1] == '.'){
board[y][x-1] = '1';
ones++;
}
if(x < n-1 && board[y][x+1] == '.'){
board[y][x+1] = '1';
ones++;
}
}
}
}
if(k == 1){
cout << ones << "\n";
return 0;
}
long long twos = 0;
for(int y=0; y<n; y++){
for(int x=0; x<n; x++){
if(board[y][x]=='1'){
if(y > 0 && board[y-1][x] == '.'){
// board[y-1][x] = '2';
// second[y-1][x]++;
twos++;
}
if(y < n-1 && board[y+1][x] == '.'){
// board[y+1][x] = '2';
// second[y+1][x]++;
twos++;
}
if(x > 0 && board[y][x-1] == '.'){
// board[y][x-1] = '2';
// second[y][x-1]++;
twos++;
}
if(x < n-1 && board[y][x+1] == '.'){
// board[y][x+1] = '2';
// second[y][x+1]++;
twos++;
}
}
}
}
cout << nChoosek(ones, 2) % 100000007 + twos << "\n";
}
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 | #include <iostream> using namespace std; char board[3000][3000]; unsigned nChoosek( unsigned n, unsigned k ) { if (k > n) return 0; if (k * 2 > n) k = n-k; if (k == 0) return 1; int result = n; for( int i = 2; i <= k; ++i ) { result *= (n-i+1); result /= i; result &= 100000007; } return result; } int main(){ int n,k; cin >> n >> k; for(int y=0; y<n; y++){ for(int x=0; x<n; x++){ char c; cin >> board[y][x]; } } long long ones = 0; for(int y=0; y<n; y++){ for(int x=0; x<n; x++){ if(board[y][x]=='#'){ if(y > 0 && board[y-1][x] == '.'){ board[y-1][x] = '1'; ones++; } if(y < n-1 && board[y+1][x] == '.'){ board[y+1][x] = '1'; ones++; } if(x > 0 && board[y][x-1] == '.'){ board[y][x-1] = '1'; ones++; } if(x < n-1 && board[y][x+1] == '.'){ board[y][x+1] = '1'; ones++; } } } } if(k == 1){ cout << ones << "\n"; return 0; } long long twos = 0; for(int y=0; y<n; y++){ for(int x=0; x<n; x++){ if(board[y][x]=='1'){ if(y > 0 && board[y-1][x] == '.'){ // board[y-1][x] = '2'; // second[y-1][x]++; twos++; } if(y < n-1 && board[y+1][x] == '.'){ // board[y+1][x] = '2'; // second[y+1][x]++; twos++; } if(x > 0 && board[y][x-1] == '.'){ // board[y][x-1] = '2'; // second[y][x-1]++; twos++; } if(x < n-1 && board[y][x+1] == '.'){ // board[y][x+1] = '2'; // second[y][x+1]++; twos++; } } } } cout << nChoosek(ones, 2) % 100000007 + twos << "\n"; } |
English