Facebook
From Tinct Meerkat, 6 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 272
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef int long long ll;
  5. char tab[3000][3000];
  6. short int mask[3000][3000][9];
  7. short int nx[] = {-1, 1, 0, 0};
  8. short int ny[] = {0, 0, 1, -1};
  9. int n, k;
  10. short int p[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
  11. short int pp[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
  12. short int bad[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
  13. const long long int c = 1e9 + 7;
  14.  
  15. bool if_fits(int x, int y){
  16.     if(x >= 0 && x < n && y >= 0 && y < n)
  17.         return 1;
  18.     else
  19.         return 0;
  20. }
  21. void neigh(int x, int y, char letter){
  22.     p[8] = 0;
  23.     for(int i = 0; i < 4; i++)
  24.     {
  25.         if(if_fits((x + nx[i]),(y + ny[i])) && tab[x + nx[i]][y + ny[i]] == letter)
  26.         {
  27.             p[2 * i] = x + nx[i];
  28.             p[2 * i + 1] = y + ny[i];
  29.             p[8] += 1;
  30.         }else{
  31.             p[2 * i] = -1;
  32.         }
  33.     }
  34. }
  35. void neigh2(int x, int y, char letter){
  36.     pp[8] = 0;
  37.     for(int i = 0; i < 4; i++)
  38.     {
  39.         if(if_fits((x + nx[i]),(y + ny[i])) && tab[x + nx[i]][y + ny[i]] == letter)
  40.         {
  41.             pp[2 * i] = x + nx[i];
  42.             pp[2 * i + 1] = y + ny[i];
  43.             pp[8] += 1;
  44.         }else{
  45.             pp[2 * i] = -1;
  46.         }
  47.     }
  48. }
  49. void zero_bad(){
  50.     for(int i = 0; i < 16; i++)
  51.     {
  52.         bad[i] = -1;
  53.     }
  54. }
  55. int check_bad(int x, int y){
  56.     int res = 0;
  57.     for(int i = 0; i < 8; i++)
  58.     {
  59.         if(bad[2 * i] != -1)
  60.             {
  61.                 if(bad[2 * i] == x && bad[2 * i + 1] == y)
  62.                     res += 1;
  63.             }
  64.     }
  65.     return res;
  66. }
  67. void chceck_bad_for_replies(){
  68.     for(int i = 0; i < 8; i++)
  69.     {
  70.         for(int j = i + 1; j < 8; j++)
  71.         {
  72.             if(bad[2 * i] == bad[2 * j] && bad[2 * i + 1] == bad[2 * j + 1])
  73.                 bad[2 * j] = -1;
  74.         }
  75.     }
  76. }
  77. int count_bad(){
  78.     int res = 0;
  79.     for(int i = 0; i < 8; i++)
  80.     {
  81.         if(bad[2 * i] != -1)
  82.             res += 1;
  83.     }
  84.     return res;
  85. }
  86.  
  87.  
  88. int main()
  89. {
  90.     cin >> n >> k;
  91.     for(int i = 0; i < n; i++)
  92.     {
  93.         for(int j = 0; j < n; j++)
  94.         {
  95.             cin >> tab[i][j];
  96.             for(int l = 0; l < 27; l++)
  97.             {
  98.                 mask[i][j][l] = 0;
  99.             }
  100.         }
  101.     }//wczytanie
  102.    
  103.     for(int x = 0; x < n; x++)
  104.     {
  105.         for(int y = 0; y < n; y++)
  106.         {
  107.             if(tab[x][y] == '.')
  108.             {
  109.                 for(int i = 0; i < 4; i++)
  110.                 {
  111.                     if(if_fits(x + nx[i], y + ny[i]) && tab[x + nx[i]][y + ny[i]] == '#')
  112.                     {
  113.                         tab[x][y] = 'a';
  114.                     }
  115.                     else
  116.                         mask[x][y][2 * i] = -1;
  117.                 }
  118.             }
  119.         }
  120.     }//wpisanie a
  121.    
  122.     for(int x = 0; x < n; x++)
  123.     {
  124.         for(int y = 0; y < n; y++)
  125.         {
  126.             if(tab[x][y] == '.')
  127.             {
  128.                 for(int i = 0; i < 4; i++)
  129.                 {
  130.                     if(if_fits(x + nx[i], y + ny[i]) && tab[x + nx[i]][y + ny[i]] == 'a')
  131.                     {
  132.                         mask[x][y][8] += 1;
  133.                         mask[x][y][2 * i] = x + nx[i];
  134.                         mask[x][y][2 * i + 1] = y + ny[i];
  135.                         tab[x][y] = 'b';
  136.                     }
  137.                     else
  138.                         mask[x][y][2 * i] = -1;
  139.                 }
  140.             }
  141.         }
  142.     }//wpisanie b
  143.    
  144.     for(int x = 0; x < n; x++)
  145.     {
  146.         for(int y = 0; y < n; y++)
  147.         {
  148.             if(tab[x][y] == '.')
  149.             {
  150.                 for(int i = 0; i < 4; i++)
  151.                 {
  152.                     if(if_fits(x + nx[i], y + ny[i]) && tab[x + nx[i]][y + ny[i]] == 'b')
  153.                     {
  154.                         mask[x][y][8] += 1;
  155.                         mask[x][y][2 * i] = x + nx[i];
  156.                         mask[x][y][2 * i + 1] = y + ny[i];
  157.                         tab[x][y] = 'c';
  158.                     }
  159.                     else
  160.                         mask[x][y][2 * i] = -1;
  161.                 }
  162.             }
  163.         }
  164.     }//wpisanie c
  165.    
  166.     for(int x = 0; x < n; x++)
  167.     {
  168.         for(int y = 0; y < n; y++)
  169.         {
  170.             if(tab[x][y] == '.')
  171.             {
  172.                 for(int i = 0; i < 4; i++)
  173.                 {
  174.                     if(if_fits(x + nx[i], y + ny[i]) && tab[x + nx[i]][y + ny[i]] == 'c')
  175.                     {
  176.                         mask[x][y][8] += 1;
  177.                         mask[x][y][2 * i] = x + nx[i];
  178.                         mask[x][y][2 * i + 1] = y + ny[i];
  179.                         tab[x][y] = 'd';
  180.                     }
  181.                     else
  182.                         mask[x][y][2 * i] = -1;
  183.                 }
  184.             }
  185.         }
  186.     }//wpisanie d
  187.    
  188.     for(int x = 0; x < n; x++)
  189.     {
  190.         for(int y = 0; y < n; y++)
  191.         {
  192.             if(tab[x][y] == 'a')
  193.             {
  194.                 for(int i = 0; i < 4; i++)
  195.                 {
  196.                     if(if_fits(x + nx[i], y + ny[i]) && tab[x + nx[i]][y + ny[i]] == 'b')
  197.                     {
  198.                         mask[x][y][8] += 1;
  199.                         mask[x][y][2 * i] = x + nx[i];
  200.                         mask[x][y][2 * i + 1] = y + ny[i];
  201.                     }
  202.                     else
  203.                         mask[x][y][2 * i] = -1;
  204.                 }
  205.             }
  206.         }
  207.     }//poprawienie maski dla a
  208.    
  209.     long long int res_a = 0;
  210.     for(int x = 0; x < n; x++)
  211.     {
  212.         for(int y = 0; y < n; y++)
  213.         {
  214.             if(tab[x][y] == 'a')
  215.                 res_a += 1;
  216.         }
  217.     }//res_a
  218.  
  219.     if(k == 1)
  220.     {
  221.         cout << res_a;
  222.     }
  223.    
  224.     if(k == 2)
  225.     {
  226.         long long int res_b = res_a * (res_a - 1) / 2;
  227.         for(int x = 0; x < n; x++)
  228.         {
  229.             for(int y = 0; y < n; y++)
  230.             {
  231.                 if(tab[x][y] == 'b')
  232.                     res_b += (ll)mask[x][y][8];
  233.             }
  234.         }
  235.         cout << res_b;
  236.     }
  237.    
  238.     if(k == 3)
  239.     {
  240.         long long int tp;
  241.         long long int res_c = (((((res_a % c) * ((res_a - 1) % c)) % c) * ((res_a - 2) % c)) % c) * 166666668; // a-a-a
  242.         res_c = res_c % c;
  243.         for(int x = 0; x < n; x++)
  244.         {
  245.             for(int y = 0; y < n; y++)
  246.             {
  247.                 if(tab[x][y] == 'c')
  248.                 {
  249.                     for(int i = 0; i < 4; i++)
  250.                     {
  251.                         if(mask[x][y][2 * i] != -1)
  252.                         {
  253.                             res_c += (ll)mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][8];
  254.                             res_c = res_c % c;
  255.                         }
  256.                     }
  257.                    
  258.                 }
  259.             }
  260.         }//a-b-c
  261.         for(int x = 0; x < n; x++)
  262.         {
  263.             for(int y = 0; y < n; y++)
  264.             {
  265.                 tp = 0;
  266.                 if(tab[x][y] == 'b')
  267.                 {
  268.                     res_c += (ll)(mask[x][y][8] * (mask[x][y][8] - 1) / 2);
  269.                     tp = max(0ll,res_a - (ll)mask[x][y][8]);
  270.                     res_c = res_c % c;
  271.                     res_c += (ll)mask[x][y][8] * tp;
  272.                     res_c = res_c % c;
  273.                 }
  274.             }
  275.         }//a-a-b
  276.         for(int x = 0; x < n; x++)
  277.         {
  278.             for(int y = 0; y < n; y++)
  279.             {
  280.                 if(tab[x][y] == 'a')
  281.                 {
  282.                     res_c += (ll)(mask[x][y][8] * (mask[x][y][8] - 1) / 2);
  283.                     res_c = res_c % c;
  284.                 }
  285.             }
  286.         }//a-b-b
  287.         for(int x = 0; x < n; x++)
  288.         {
  289.             for(int y = 0; y < n; y++)
  290.             {
  291.                 if(tab[x][y] == 'a')
  292.                 {
  293.                     for(int i = 0; i < 4; i++)
  294.                     {
  295.                         if(mask[x][y][2 * i] != -1)
  296.                         {
  297.                             neigh(mask[x][y][2 * i],mask[x][y][2 * i + 1],'b');
  298.                             res_c += (ll)p[8];
  299.                         }
  300.                     }
  301.                 }
  302.             }
  303.         }//a-b-b(c)
  304.         cout << res_c;
  305.     }
  306.    
  307.     if(k == 4)
  308.     {
  309.         long long int tp;
  310.         long long int res_d = (((((((res_a % c) * ((res_a - 1) % c)) % c) * ((res_a - 2) % c)) % c) * ((res_a - 3) % c)) % c) * 41666667; // a-a-a-a
  311.         res_d = res_d % c;
  312.         for(int x = 0; x < n; x++)
  313.         {
  314.             for(int y = 0; y < n; y++)
  315.             {
  316.                 if(tab[x][y] == 'd')
  317.                 {
  318.                     for(int i = 0; i < 4; i++)
  319.                     {
  320.                         if(mask[x][y][2 * i] != -1)
  321.                         {
  322.                             for(int j = 0; j < 4; j++)
  323.                             {
  324.                                 if(mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][2 * j] != -1)
  325.                                 {
  326.                                     res_d += (ll)mask [mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][2 * j]] [mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][2 * j + 1]] [8];
  327.                                     res_d = res_d % c;
  328.                                     cout << mask [mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][2 * j]] [mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][2 * j + 1]] [8] << "|";
  329.                                 }
  330.                             }
  331.                         }
  332.                     }
  333.                    
  334.                 }
  335.             }
  336.         }//a-b-c-d
  337.         for(int x = 0; x < n; x++)
  338.         {
  339.             for(int y = 0; y < n; y++)
  340.             {
  341.                 tp = 0;
  342.                 if(tab[x][y] == 'b')
  343.                 {
  344.                     res_d += (ll)(mask[x][y][8] * (mask[x][y][8] - 1) * (mask[x][y][8] - 2) / 6);
  345.                     tp = max(0ll,res_a - (ll)mask[x][y][8]);
  346.                     res_d += tp * (ll)(mask[x][y][8] * (mask[x][y][8] - 1) / 2);
  347.                     res_d = res_d % c;
  348.                     res_d += tp * (tp - 1) * (ll)mask[x][y][8] / 2;
  349.                     res_d = res_d % c;
  350.                 }
  351.             }
  352.         }//a-a-a-b
  353.         for(int x = 0; x < n; x++)
  354.         {
  355.             for(int y = 0; y < n; y++)
  356.             {
  357.                 if(tab[x][y] == 'a')
  358.                 {
  359.                     if(mask[x][y][8] >= 1)
  360.                     {
  361.                         zero_bad();
  362.                         for(int i = 0; i < 4; i++)
  363.                         {
  364.                             if(mask[x][y][2 * i] != -1)
  365.                             {
  366.                                 neigh(mask[x][y][2 * i],mask[x][y][2 * i + 1],'b');
  367.                                 res_d += (ll)(p[8] * (p[8] - 1) / 2);
  368.                                 for(int j = 0; j < 4; j++)
  369.                                 {
  370.                                     if(p[2 * j] != -1)
  371.                                     {
  372.                                         neigh2(p[2 * j],p[2 * j + 1],'b');
  373.                                         int s = 0;
  374.                                         for(int h = 0; h < 4; h++)
  375.                                             s += check_bad(pp[2 * h],pp[2 * h + 1]);
  376.                                         res_d += max(0ll,(ll)(pp[8] - 1 - s));
  377.                                     }
  378.                                 }
  379.                                 bad[2 * i] = mask[x][y][2 * i];
  380.                                 bad[2 * i + 1] = mask[x][y][2 * i + 1];
  381.                                 chceck_bad_for_replies();
  382.                             }
  383.                             res_d = res_d % c;
  384.                         }
  385.                     }
  386.                     if(mask[x][y][8] >= 2)
  387.                     {
  388.                         zero_bad();
  389.                         int index = 0;
  390.                         for(int i = 0; i < 4; i++)
  391.                         {
  392.                             if(mask[x][y][2 * i] != -1)
  393.                             {
  394.                                 for(int j = i + 1; j < 4; j++)
  395.                                 {
  396.                                     if(mask[x][y][2 * j] != -1)
  397.                                     {
  398.                                         neigh(mask[x][y][2 * i],mask[x][y][2 * i + 1],'b');
  399.                                         res_d += (ll)(p[8] - check_bad(mask[x][y][2 * i],mask[x][y][2 * i + 1]));
  400.                                         for(int h = index * 4; (h < (index + 1) * 4 && index < 2); h++)
  401.                                         {
  402.                                             if(p[(2 * h) % 8] != -1)
  403.                                             {
  404.                                                 bad[2 * h] = p[(2 * h) % 8];
  405.                                                 bad[2 * h + 1] = p [(2 * h + 1) % 8];
  406.                                             }
  407.                                         }
  408.                                         chceck_bad_for_replies();
  409.                                         index ++;
  410.                                     }
  411.                                 }
  412.                             }
  413.                         }
  414.                         res_d = res_d % c;
  415.                     }
  416.                     if(mask[x][y][8] == 3)
  417.                     {
  418.                         res_d += 1;
  419.                     }
  420.                 }
  421.                 res_d = res_d % c;
  422.             }
  423.         }//a-b-b-b
  424.         for(int x = 0; x < n; x++)
  425.         {
  426.             for(int y = 0; y < n; y++)
  427.             {
  428.                 if(tab[x][y] == 'a')
  429.                 {
  430.                     if(mask[x][y][8] > 0)
  431.                     {  
  432.                         for(int i = 0; i < 4; i++)
  433.                         {
  434.                             if(mask[x][y][2 * i] != -1)
  435.                             {
  436.                                 neigh(mask[x][y][2 * i],mask[x][y][2 * i + 1],'b');
  437.                                 for(int j = 0; j < 4; j++)
  438.                                 {
  439.                                     if(p[2 * j] != -1)
  440.                                     {
  441.                                         neigh2(p[2 * j],p[2 * j + 1],'c');
  442.                                         res_d += (ll)(pp[8] * p[8]);
  443.                                     }
  444.                                 }
  445.                             }
  446.                         }
  447.                         zero_bad();
  448.                         for(int h = 0; h < 4; h++)
  449.                         {
  450.                           bad[2 * h] = mask[x][y][2 * h];
  451.                           bad[2 * h + 1] = mask[x][y][2 * h + 1];
  452.                         }
  453.                         for(int i = 0; i < 4; i++)
  454.                         {
  455.                             if(mask[x][y][2 * i] != -1)
  456.                             {
  457.                                 neigh(mask[x][y][2 * i],mask[x][y][2 * i + 1],'c');
  458.                                 for(int j = 0; j < 4; j++)
  459.                                 {
  460.                                     if(p[2 * j] != -1)
  461.                                     {
  462.                                         neigh2(p[2 * j],p[2 * j + 1],'b');
  463.                                         for(int h = 0; h < 4; h++)
  464.                                         {
  465.                                             if(pp[2 * h] != -1)
  466.                                             {
  467.                                                 if(check_bad(pp[2 * h],pp[2 * h + 1]) == 0)
  468.                                                     res_d += 1;
  469.                                             }
  470.                                         }
  471.                                     }
  472.                                 }
  473.                             }
  474.                         }
  475.                    
  476.                      
  477.                     }
  478.                 }
  479.             }
  480.         }//a-b-c-b
  481.         for(int x = 0; x < n; x++)
  482.         {
  483.             for(int y = 0; y < n; y++)
  484.             {
  485.                 if(tab[x][y] == 'a')
  486.                 {
  487.                     if(mask[x][y][8] >= 2)
  488.                     {
  489.                         for(int i = 0; i < 4; i++)
  490.                         {
  491.                             for(int j = i + 1; j < 4; j++)
  492.                             {
  493.                                 if(mask[x][y][2 * i] != -1 && mask[x][y][2 * j] != -1)
  494.                                 {
  495.                                     int b_x1 = mask[x][y][2 * i];
  496.                                     int b_y1 = mask[x][y][2 * i + 1];
  497.                                     int b_x2 = mask[x][y][2 * j];
  498.                                     int b_y2 = mask[x][y][2 * j + 1];
  499.                                     int count = 0;
  500.                                     neigh(b_x1,b_y1,'c');
  501.                                     zero_bad();
  502.                                     for(int m = 0; m < 4; m++)
  503.                                     {
  504.                                         bad[2 * m] = p[2 * m];
  505.                                         bad[2 * m + 1] = p[2 * m + 1];
  506.                                     }
  507.                                     neigh2(b_x2,b_y2,'c');
  508.                                     for(int m = 0; m < 4; m++)
  509.                                     {
  510.                                         count += check_bad(pp[2 * m],pp[2 * m + 1]);
  511.                                     }
  512.                                 res_d += (ll)(p[8] + pp[8] - count);
  513.                                 res_d = res_d % c;
  514.                                 }
  515.                             }
  516.                         }
  517.                     }
  518.                 }
  519.             }
  520.         }//a-b-b-c
  521.         for(int x = 0; x < n; x++)
  522.         {
  523.             for(int y = 0; y < n; y++)
  524.             {
  525.                 if(tab[x][y] == 'a')
  526.                 {
  527.                     if(mask[x][y][8] > 0)
  528.                     {
  529.                         for(int i = 0; i < 4; i++)
  530.                         {
  531.                             if(mask[x][y][2 * i] != -1)
  532.                             {
  533.                                 neigh(mask[x][y][2 * i],mask[x][y][2 * i],'c');
  534.                                 res_d += (ll)(p[8] * (p[8] - 1));
  535.                                 for(int j = 0; j < 4; j++)
  536.                                 {
  537.                                     if(p[2 * j] != -1)
  538.                                     {
  539.                                         neigh2(p[2 * j],p[2 * j + 1],'c');
  540.                                         res_d += (ll)(pp[8] - 1);
  541.                                     }
  542.                                 }
  543.                             }
  544.                             res_d = res_d % c;
  545.                         }
  546.                     }
  547.                 }
  548.             }
  549.         }//a-b-c-c
  550.         for(int x = 0; x < n; x++)
  551.         {
  552.             for(int y = 0; y < n; y++)
  553.             {
  554.                 if(tab[x][y] == 'c')
  555.                 {
  556.                     for(int i = 0; i < 4; i++)
  557.                     {
  558.                         tp = 0;
  559.                         if(mask[x][y][2 * i] != -1)
  560.                         {
  561.                             tp = max(0ll,res_a - (ll)mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][8]);
  562.                             res_d += (ll)(mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][8] * (mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][8] - 1) / 2);
  563.                             res_d += (ll)(mask[mask[x][y][2 * i]][mask[x][y][2 * i + 1]][8]) * tp;
  564.                             res_d = res_d % c;
  565.                         }
  566.                     }
  567.                    
  568.                 }
  569.             }
  570.         }//a-a-b-c
  571.         for(int x = 0; x < n; x++)
  572.         {
  573.             for(int y = 0; y < n; y++)
  574.             {
  575.                 tp = 0;
  576.                 if(tab[x][y] == 'a')
  577.                 {
  578.                     int inter = 0;
  579.                     for(int i = 0; i < 4; i++)
  580.                     {
  581.                         for(int j = i + 1; j < 4; j++)
  582.                         {
  583.                             if(mask[x][y][2 * i] != -1 && mask[x][y][2 * j] != -1)
  584.                             {
  585.                                 int b_x1 = mask[x][y][2 * i];
  586.                                 int b_y1 = mask[x][y][2 * i + 1];
  587.                                 int b_x2 = mask[x][y][2 * j];
  588.                                 int b_y2 = mask[x][y][2 * j + 1];
  589.                                 int count = 0;
  590.                                 neigh(b_x1,b_y1,'a');
  591.                                 zero_bad();
  592.                                 for(int m = 0; m < 4; m++)
  593.                                 {
  594.                                     bad[2 * m] = p[2 * m];
  595.                                     bad[2 * m + 1] = p[2 * m + 1];
  596.                                 }
  597.                                 neigh2(b_x2,b_y2,'a');
  598.                                 for(int m = 0; m < 4; m++)
  599.                                 {
  600.                                     count += check_bad(pp[2 * m],pp[2 * m + 1]);
  601.                                 }
  602.                                 tp = max(0ll, res_a - (ll)(mask[b_x1][b_y1][8] + mask[b_x2][b_y2][8] - count));
  603.                                 res_d += tp;
  604.                                 res_d = res_d % c;
  605.                                 }
  606.                             }
  607.                         }
  608.                     }
  609.                 }
  610.             } //(a-(b-b) a)
  611.         long long int sq = 0;
  612.         long long int sm = 0;
  613.         for(int x = 0; x < n; x++)
  614.         {
  615.             for(int y = 0; y < n; y++)
  616.             {
  617.                 if(tab[x][y] == 'a')
  618.                 {
  619.                     sq += (ll)(mask[x][y][8] * mask[x][y][8]);
  620.                     sm += (ll)mask[x][y][8];
  621.                 }
  622.             }
  623.         }//pairs
  624.         long long int pairs = (sm * sm - sq) / 2;
  625.         long long int minus = 0;
  626.         for(int x = 0; x < n; x++)
  627.         {
  628.             for(int y = 0; y < n; y++)
  629.             {
  630.                 if(tab[x][y] == 'b')
  631.                 {
  632.                     if(mask[x][y][8] == 2)
  633.                         minus += 1;
  634.                     if(mask[x][y][8] == 3)
  635.                         minus += 3;
  636.                 }
  637.             }
  638.         }//minus
  639.         res_d += (pairs - minus) % c;
  640.         res_d = res_d % c;
  641.         //(a-b)(a-b)
  642.         for(int x = 0; x < n; x++)
  643.         {
  644.             for(int y = 0; y < n; y++)
  645.             {
  646.                 if(tab[x][y] == 'b')
  647.                 {
  648.                     zero_bad();
  649.                     for(int h = 0; h < 4; h++)
  650.                     {
  651.                         bad[2 * h] = mask[x][y][2 * h];
  652.                         bad[2 * h + 1] = mask[x][y][2 * h + 1];
  653.                     }
  654.                     neigh(x,y,'b');
  655.                     for(int i = 0; i < 4; i++)
  656.                     {
  657.                         if(p[2 * i] != -1)
  658.                         {
  659.                             int ch = 0;
  660.                             neigh2(p[2 * i],p[2 * i + 1], 'a');
  661.                             for(int j = 0; j < 4; j++)
  662.                             {
  663.                                 ch += check_bad(pp[2 * j],pp[2 * j + 1]);
  664.                             }
  665.                             if(ch == 0)
  666.                                 res_d += (ll)(mask[x][y][8] * (mask[x][y][8] - 1) / 2);
  667.                         }
  668.                     }
  669.                     res_d = res_d % c;
  670.                 }
  671.             }
  672.         }//(a-a)-b-b
  673.         for(int x = 0; x < n; x++)
  674.         {
  675.             for(int y = 0; y < n; y++)
  676.             {
  677.                 if(tab[x][y] == 'b')
  678.                 {
  679.                     zero_bad();
  680.                     for(int h = 0; h < 4; h++)
  681.                     {
  682.                         bad[2 * h] = mask[x][y][2 * h];
  683.                         bad[2 * h + 1] = mask[x][y][2 * h + 1];
  684.                     }
  685.                     neigh(x,y,'b');
  686.                     for(int i = 0; i < 4; i++)
  687.                     {
  688.                         if(p[2 * i] != -1)
  689.                         {
  690.                             ll tp = 0;
  691.                             for(int h = 0; h < 4; h++)
  692.                             {
  693.                                 bad[2 * h + 8] = mask[p[2 * i]][p[2 * i + 1]][2 * h];
  694.                                 bad[2 * h + 9] = mask[p[2 * i]][p[2 * i + 1]][2 * h + 1];
  695.                             }
  696.                             chceck_bad_for_replies();
  697.                             tp = max(0ll,res_a - (ll)count_bad());
  698.                             res_d += tp * (ll)mask[x][y][8];
  699.                             res_d = res_d % c;
  700.                         }
  701.                     }
  702.                 }
  703.             }
  704.         }//a-b-b a
  705.        
  706.         cout << res_d;
  707.     }
  708.    
  709.     return 0;
  710. }
  711.  
  712.