#include<iostream> using namespace std; const int mod = 1000000007; long long finito, how_mucz; int n, k; char a; int tab[3002][3002]; long long silnia[5000000]; long long Pow(long long base, long long power) { if(power==0)return 1; if(power%2==0) { long long result=Pow(base,power/2); return (result*result)%mod; } else return (base*Pow(base,power-1))%mod; } void Silnia() { silnia[0] = 1; silnia[1] = 1; long long koniec = 1; for(int i = 2; i<5000000; i++) { koniec*=i; koniec%=mod; silnia[i] = koniec; } } int main() { ios_base::sync_with_stdio(0); cin >> n >> k; Silnia(); if(k == 3 || k == 4) { cout << "No niestety taki dobry to ja nie jestem"; return 0; } for(int i = 1; i<=n; i++) { for(int j = 1; j<=n; j++) { cin >> a; if(a == '#') tab[i][j] = 1; } } for(int i = 1; i<=n; i++) { for(int j = 1; j<=n; j++) { if(tab[i][j] == 0 && (tab[i-1][j] == 1 || tab[i][j-1] == 1 || tab[i][j+1] == 1 || tab[i+1][j] == 1 )) { how_mucz++; tab[i][j] = 2; } } } if(k == 1) { cout << how_mucz; return 0; } if(k == 2) { finito+=(silnia[how_mucz])*(Pow((silnia[2]*silnia[how_mucz-2])%mod, mod-2)%mod)%mod; how_mucz = 0; for(int i = 1; i<=n; i++) { for(int j = 1; j<=n; j++) { if(tab[i][j] == 0 && tab[i-1][j] == 2) { how_mucz++; } if(tab[i][j] == 0 && tab[i+1][j] == 2) { how_mucz++; } if(tab[i][j] == 0 && tab[i][j+1] == 2) { how_mucz++; } if(tab[i][j] == 0 && tab[i][j-1] == 2) { how_mucz++; } } } finito+=how_mucz; cout << finito; 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 | #include<iostream> using namespace std; const int mod = 1000000007; long long finito, how_mucz; int n, k; char a; int tab[3002][3002]; long long silnia[5000000]; long long Pow(long long base, long long power) { if(power==0)return 1; if(power%2==0) { long long result=Pow(base,power/2); return (result*result)%mod; } else return (base*Pow(base,power-1))%mod; } void Silnia() { silnia[0] = 1; silnia[1] = 1; long long koniec = 1; for(int i = 2; i<5000000; i++) { koniec*=i; koniec%=mod; silnia[i] = koniec; } } int main() { ios_base::sync_with_stdio(0); cin >> n >> k; Silnia(); if(k == 3 || k == 4) { cout << "No niestety taki dobry to ja nie jestem"; return 0; } for(int i = 1; i<=n; i++) { for(int j = 1; j<=n; j++) { cin >> a; if(a == '#') tab[i][j] = 1; } } for(int i = 1; i<=n; i++) { for(int j = 1; j<=n; j++) { if(tab[i][j] == 0 && (tab[i-1][j] == 1 || tab[i][j-1] == 1 || tab[i][j+1] == 1 || tab[i+1][j] == 1 )) { how_mucz++; tab[i][j] = 2; } } } if(k == 1) { cout << how_mucz; return 0; } if(k == 2) { finito+=(silnia[how_mucz])*(Pow((silnia[2]*silnia[how_mucz-2])%mod, mod-2)%mod)%mod; how_mucz = 0; for(int i = 1; i<=n; i++) { for(int j = 1; j<=n; j++) { if(tab[i][j] == 0 && tab[i-1][j] == 2) { how_mucz++; } if(tab[i][j] == 0 && tab[i+1][j] == 2) { how_mucz++; } if(tab[i][j] == 0 && tab[i][j+1] == 2) { how_mucz++; } if(tab[i][j] == 0 && tab[i][j-1] == 2) { how_mucz++; } } } finito+=how_mucz; cout << finito; return 0; } } |