Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define FN "algoritm"
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7.  
  8. const ll INF = 1e16;
  9. const int mod = 1e9 + 7;
  10.  
  11. ll ans = 0;
  12. ll n, m, k, d;
  13. ll arr[15][170];
  14.  
  15. ll play(bool a[])
  16. {
  17.     vector <pair <ll, ll > > b;
  18.     ll c;
  19.     ll answ = 1;
  20.     for (int i = 0; i < m; i++)
  21.     {
  22.         if (a[i])
  23.         {
  24.             c = 0;
  25.             for (int j = 0; j < n; j++)
  26.             {
  27.                 b.clear();
  28.                 if (arr[i][j] < d && arr[i][j] + d > k)
  29.                 {
  30.                     b.clear();
  31.                     b.push_back({ 0ll, k });
  32.                     break;
  33.                 }
  34.                 if (arr[i][j] < d)
  35.                 {
  36.                     b.push_back({ arr[i][j] + k - d, k - 1 });
  37.                     b.push_back({0, arr[i][j] + d});
  38.                 }
  39.                 else if (arr[i][j] + d > k)
  40.                 {
  41.                     b.push_back({ arr[i][j], k - 1 });
  42.                     b.push_back({ 0, arr[i][j] + d - k});
  43.                 }
  44.                 else
  45.                 {
  46.                     b.push_back({ arr[i][j] - d, arr[i][j] + d});
  47.                 }
  48.             }
  49.             sort(b.begin(), b.end());
  50.             int l = -1, r = -2;
  51.             for (int j = 0; j < b.size(); j++)
  52.             {
  53.                 if (b[j].first <= r)
  54.                     r = b[j].second;
  55.                 else
  56.                 {
  57.                     c += r - l + 1;
  58.                     l = b[j].first;
  59.                     r = b[j].second;
  60.                 }
  61.             }
  62.             c += r - l + 1;
  63.             l = b[b.size() - 1].first;
  64.             r = b[b.size() - 1].second;
  65.             answ *= c;
  66.         }
  67.     }
  68.     cout << answ << endl;
  69.     return answ;
  70. }
  71.  
  72. void rec(int i, bool a[], int k)
  73. {
  74.     if (i == m)
  75.     {
  76.         if (k % 2 == 1)
  77.             ans = (ans + play(a)) % mod;
  78.         else
  79.             ans = (ans - play(a)) % mod;
  80.         cout << '=' << ans;
  81.     //    for (int i = 0; i < m; i++)
  82.   //          cout << a[i] << ' ';
  83.  //           cout  << endl << ans;
  84.         return;
  85.     }
  86.     a[i] = 1;
  87.     rec(i + 1, a, k + 1);
  88.     a[i] = 0;
  89.     rec(i + 1, a, k);
  90. }
  91.  
  92. signed main()
  93. {
  94. #ifdef PC
  95.     freopen("in", "r", stdin);
  96. #else
  97.     freopen(FN".in", "r", stdin);
  98.     freopen(FN".out", "w", stdout);
  99. #endif
  100.     ios_base::sync_with_stdio(0);
  101.     cin >> n >> m >> k >> d;
  102.     for (int i = 0; i < m; i++)
  103.     {
  104.         for (int j = 0; j < n; j++)
  105.             cin >> arr[i][j];
  106.     }
  107.     bool a[12];
  108.     rec(0, a, 0);
  109.     cout << ans;
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement