forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
61 lines (56 loc) · 1.28 KB
/
E.cpp
File metadata and controls
61 lines (56 loc) · 1.28 KB
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
#include <bits/stdc++.h>
using namespace std;
const int BASE = int(1e9) + 7;
int n, m, k, d, a[22][111];
int solve(int mask, int j)
{
if (d * 2 + 1 >= k)
return k;
int bit = __builtin_popcount(mask);
map <int,int> cnt;
cnt[k] = 0;
for (int i = 0; i < m; i++)
if (mask >> i & 1)
{
int from = (a[i][j] - d + k) % k;
int to = (a[i][j] + d) % k;
cnt[from]++;
cnt[to + 1]--;
if (from > to)
cnt[0]++;
}
int last = 0, res = 0, cur = 0;
for (auto u : cnt)
{
if (cur == bit) res += u.first - last;
cur += u.second;
last = u.first;
}
return res;
}
int main()
{
freopen("cifrul.in", "r", stdin);
freopen("cifrul.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int test;
cin >> test;
while (test--)
{
cin >> n >> m >> k >> d;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> a[i][j];
long long ans = 0;
for (int mask = 1; mask < 1 << m; mask++)
{
long long ways = 1;
for (int j = 0; j < n; j++)
ways = ways * solve(mask, j) % BASE;
if (__builtin_popcount(mask) % 2) ans = (ans + ways) % BASE;
else ans = (ans - ways + BASE) % BASE;
}
cout << ans << endl;
}
}