Facebook
From Corrupt Pudu, 6 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 254
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. int n, k;
  6. ll p, d, t;
  7. const ll m = 1e9 + 7;
  8. int T[3001][3001];
  9. int d_x[] = {-1, 1, 0, 0};
  10. int d_y[] = {0, 0, 1, -1};
  11.  
  12. ll counter_p(ll ctr) {
  13.         ll res = 0;
  14.         for(int i = 1; i <= n; i++) {
  15.                 for(int j = 1; j <= n; j++) {
  16.                         if(T[i][j] == 'w') {
  17.                                 for(int q = 0; q < 4; q++) {
  18.                                         if(1 <= i + d_x[q] && i + d_x[q] <= n) {
  19.                                                 if(1 <= j + d_y[q] && j + d_y[q] <= n) {
  20.                                                         if(T[i + d_x[q]][j + d_y[q]] == 'k') {
  21.                                                                 T[i][j] = 'p';
  22.                                                         }
  23.                                                 }
  24.                                         }
  25.                                 }
  26.                         }
  27.                         if(T[i][j] == ctr) res++;
  28.                 }
  29.         }
  30.         return res;
  31. }
  32.  
  33. ll counter_d(ll ctr) {
  34.         ll res = 0;
  35.         for(int i = 1; i <= n; i++) {
  36.                 for(int j = 1; j <= n; j++) {
  37.                         if(T[i][j] == 'w') {
  38.                                 for(int q = 0; q < 4; q++) {
  39.                                         if(1 <= i + d_x[q] && i + d_x[q] <= n) {
  40.                                                 if(1 <= j + d_y[q] && j + d_y[q] <= n) {
  41.                                                         if(T[i + d_x[q]][j + d_y[q]] == 'p') {
  42.                                                                 T[i][j] = 'd';
  43.                                                         }
  44.                                                 }
  45.                                         }
  46.                                 }
  47.                         }
  48.                         if(T[i][j] == ctr) res++;
  49.                 }
  50.         }
  51.         return res;
  52. }
  53.  
  54. ll counter_t(ll ctr) {
  55.         ll res = 0;
  56.         for(int i = 1; i <= n; i++) {
  57.                 for(int j = 1; j <= n; j++) {
  58.                         if(T[i][j] == 'w') {
  59.                                 for(int q = 0; q < 4; q++) {
  60.                                         if(1 <= i + d_x[q] && i + d_x[q] <= n) {
  61.                                                 if(1 <= j + d_y[q] && j + d_y[q] <= n) {
  62.                                                         if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) {
  63.                                                                 T[i][j] = 't';
  64.                                                         }
  65.                                                 }
  66.                                         }
  67.                                 }
  68.                         }
  69.                         if(T[i][j] == 'd') {
  70.                                 for(int q = 0; q < 4; q++) {
  71.                                         if(1 <= i + d_x[q] && i + d_x[q] <= n) {
  72.                                                 if(1 <= j + d_y[q] && j + d_y[q] <= n) {
  73.                                                         if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) {
  74.                                                                 T[i][j] = 't'+1;
  75.                                                         }
  76.                                                 }
  77.                                         }
  78.                                 }
  79.                         }
  80.                         if(T[i][j] == ctr) res++;
  81.                 }
  82.         }
  83.         return res;
  84. }
  85.  
  86. ll sumaTakichZePiD() {
  87.         ll res = 0;
  88.         for(int i = 1; i <= n; i++) {
  89.                 for(int j = 1; j <= n; j++) {
  90.                         if(T[i][j] == 'p') {
  91.                                 for(int q = 0; q < 4; q++) {
  92.                                         if(1 <= i + d_x[q] && i + d_x[q] <= n) {
  93.                                                 if(1 <= j + d_y[q] && j + d_y[q] <= n) {
  94.                                                         if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) {
  95.                                                                 res++;
  96.                                                                 res%=m;
  97.                                                         }
  98.                                                 }
  99.                                         }
  100.                                 }
  101.                         }
  102.                 }
  103.         }
  104.         return res%m;
  105. }
  106.  
  107. ll sumaIloczynowD() {
  108.         ll res = 0;
  109.         for(int i = 1; i <= n; i++) {
  110.                 for(int j = 1; j <= n; j++) {
  111.                         if(T[i][j] == 'p') {
  112.                                 ll tmp = 0;
  113.                                 for(int q = 0; q < 4; q++) {
  114.                                         if(1 <= i + d_x[q] && i + d_x[q] <= n) {
  115.                                                 if(1 <= j + d_y[q] && j + d_y[q] <= n) {
  116.                                                         if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) {
  117.                                                                 tmp++;
  118.                                                         }
  119.                                                 }
  120.                                         }
  121.                                 }
  122.                                 res += ((((tmp%m)*((tmp-1)%m))%m)*500000004)%m;
  123.                         }
  124.                 }
  125.         }
  126.         return res%m;
  127. }
  128.  
  129. ll sumaTakichAAB() {
  130.         ll res = 0;
  131.         for(int i = 1; i <= n; i++) {
  132.                 for(int j = 1; j <= n; j++) {
  133.                         if(T[i][j] == 'd' || T[i][j] == 't'+1) {
  134.                                 ll tmp = 0;
  135.                                 for(int q = 0; q < 4; q++) {
  136.                                         if(1 <= i + d_x[q] && i + d_x[q] <= n) {
  137.                                                 if(1 <= j + d_y[q] && j + d_y[q] <= n) {
  138.                                                         if(T[i + d_x[q]][j + d_y[q]] == 'p') {
  139.                                                                 tmp++;
  140.                                                         }
  141.                                                 }
  142.                                         }
  143.                                 }
  144.                                 res += ((((tmp%m)*((tmp-1)%m))%m)*500000004)%m;
  145.                                 res += ((tmp%m)*((m+p-tmp)%m))%m;
  146.                         }
  147.                 }
  148.         }
  149.         return res%m;
  150. }
  151.  
  152. ll ileTychT() {
  153.         ll res = 0;
  154.         for(int i = 1; i <= n; i++) {
  155.                 for(int j = 1; j <= n; j++) {
  156.                         if(T[i][j] == 'p') {
  157.                                 for(int q = 0; q < 4; q++) {
  158.                                         if(1 <= i + d_x[q] && i + d_x[q] <= n) {
  159.                                                 if(1 <= j + d_y[q] && j + d_y[q] <= n) {
  160.                                                         if(T[i + d_x[q]][j + d_y[q]] == 'd' || T[i + d_x[q]][j + d_y[q]] == 't'+1) {
  161.                                                                 for(int h = 0; h < 4; h++) {
  162.                                                                         if(1 <= i + d_x[q] + d_x[h] && i + d_x[q] + d_x[h] <= n) {
  163.                                                                                 if(1 <= j + d_y[q] + d_y[h] && j + d_y[q] + d_y[h] <= n) {
  164.                                                                                         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) {
  165.                                                                                                 res++;
  166.                                                                                                 res%=m;
  167.                                                                                         }
  168.                                                                                 }
  169.                                                                         }
  170.                                                                 }
  171.                                                         }
  172.                                                 }
  173.                                         }
  174.                                 }
  175.                         }
  176.                 }
  177.         }
  178.         return res%m;
  179. }
  180.  
  181. int main() {
  182.         ios_base::sync_with_stdio(false);
  183.         cin >> n >> k;
  184.        
  185.         for(int i = 1; i <= n; i++) {
  186.                 for(int j = 1; j <= n; j++) {
  187.                         char c; cin >> c;
  188.                         if(c == '.') T[i][j] = 'w';
  189.                         else T[i][j] = 'k';
  190.                 }
  191.         }
  192.        
  193.         p = counter_p('p');
  194.         d = counter_d('d');
  195.         t = counter_t('t');
  196.        
  197.         if(k == 1) {
  198.                 cout << p % m << endl;
  199.         } else if(k == 2) {
  200.                 ll sameP = ((((p%m)*((m+p-1)%m))%m)*500000004)%m;
  201.                 cout << (sameP%m + sumaTakichZePiD()%m) % m << endl;
  202.         } else if(k == 3) {
  203.                 ll sameP = (((((p%m)*((m+p-1)%m))%m*((m+p-2)%m))%m)*166666668)%m;
  204.                 cout << (((sameP%m + sumaTakichAAB()%m)%m + sumaIloczynowD()%m)%m + ileTychT()%m)%m << endl;
  205.         } else if(k == 4) {
  206.                
  207.         }
  208.        
  209.         return 0;
  210. }
  211.