#include <bits/stdc++.h>
using namespace std;
long long n, d, arr[36][36][36][36];
long long zero, one[36][4], two[36][36][6], three[36][36][36][4], four[36][36][36][36];
int CharToInt(char c) {
if (c <= '9') return c - '0';
if (c >= 'a') return c - 'a' + 10;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> d;
for (int i = 1; i <= n; i++) {
char s[10];
cin >> s;
arr[CharToInt(s[0])][CharToInt(s[1])][CharToInt(s[2])][CharToInt(s[3])] = 1;
}
for (int i = 0; i < 36; i++) {
for (int j = 0; j < 36; j++) {
for (int k = 0; k < 36; k++) {
for (int l = 0; l < 36; l++) {
four[i][j][k][l] = arr[i][j][k][l] * (arr[i][j][k][l] + 1) / 2;
}
}
}
}
for (int i = 0; i < 36; i++) {
for (int j = 0; j < 36; j++) {
for (int k = 0; k < 36; k++) {
for (int l = 0; l < 36; l++) {
three[i][j][k][0] += arr[i][j][k][l];
three[i][j][k][1] += arr[i][j][l][k];
three[i][j][k][2] += arr[i][l][j][k];
three[i][j][k][3] += arr[l][i][j][k];
}
three[i][j][k][0] = three[i][j][k][0] * (three[i][j][k][0] + 1) / 2;
three[i][j][k][1] = three[i][j][k][1] * (three[i][j][k][1] + 1) / 2;
three[i][j][k][2] = three[i][j][k][2] * (three[i][j][k][2] + 1) / 2;
three[i][j][k][3] = three[i][j][k][3] * (three[i][j][k][3] + 1) / 2;
for (int l = 0; l < 36; l++) {
three[i][j][k][0] -= four[i][j][k][l];
three[i][j][k][1] -= four[i][j][l][k];
three[i][j][k][2] -= four[i][l][j][k];
three[i][j][k][3] -= four[l][i][j][k];
}
}
}
}
for (int i = 0; i < 36; i++) {
for (int j = 0; j < 36; j++) {
for (int k = 0; k < 36; k++) {
for (int l = 0; l < 36; l++) {
two[i][j][0] += arr[i][j][k][l];
two[i][j][1] += arr[i][k][j][l];
two[i][j][2] += arr[i][k][l][j];
two[i][j][3] += arr[k][i][j][l];
two[i][j][4] += arr[k][i][l][j];
two[i][j][5] += arr[k][l][i][j];
}
}
two[i][j][0] = two[i][j][0] * (two[i][j][0] + 1) / 2;
two[i][j][1] = two[i][j][1] * (two[i][j][1] + 1) / 2;
two[i][j][2] = two[i][j][2] * (two[i][j][2] + 1) / 2;
two[i][j][3] = two[i][j][3] * (two[i][j][3] + 1) / 2;
two[i][j][4] = two[i][j][4] * (two[i][j][4] + 1) / 2;
two[i][j][5] = two[i][j][5] * (two[i][j][5] + 1) / 2;
for (int k = 0; k < 36; k++) {
two[i][j][0] -= three[i][j][k][0];
two[i][j][1] -= three[i][k][j][0];
two[i][j][2] -= three[i][k][j][1];
two[i][j][3] -= three[k][i][j][0];
two[i][j][4] -= three[k][i][j][1];
two[i][j][5] -= three[k][i][j][2];
}
for (int l = 0; l < 36; l++) {
two[i][j][0] -= three[i][j][l][1];
two[i][j][1] -= three[i][j][l][2];
two[i][j][2] -= three[i][l][j][2];
two[i][j][3] -= three[i][j][l][3];
two[i][j][4] -= three[i][l][j][3];
two[i][j][5] -= three[l][i][j][3];
}
for (int k = 0; k < 36; k++) {
for (int l = 0; l < 36; l++) {
two[i][j][0] -= four[i][j][k][l];
two[i][j][1] -= four[i][k][j][l];
two[i][j][2] -= four[i][k][l][j];
two[i][j][3] -= four[k][i][j][l];
two[i][j][4] -= four[k][i][l][j];
two[i][j][5] -= four[k][l][i][j];
}
}
}
}
for (int i = 0; i < 36; i++) {
for (int j = 0; j < 36; j++) {
for (int k = 0; k < 36; k++) {
for (int l = 0; l < 36; l++) {
one[i][0] += arr[i][j][k][l];
one[i][1] += arr[j][i][k][l];
one[i][2] += arr[j][k][i][l];
one[i][3] += arr[j][k][l][i];
}
}
}
one[i][0] = one[i][0] * (one[i][0] + 1) / 2;
one[i][1] = one[i][1] * (one[i][1] + 1) / 2;
one[i][2] = one[i][2] * (one[i][2] + 1) / 2;
one[i][3] = one[i][3] * (one[i][3] + 1) / 2;
for (int j = 0; j < 36; j++) {
one[i][0] -= two[i][j][0];
one[i][1] -= two[j][i][0];
one[i][2] -= two[j][i][1];
one[i][3] -= two[j][i][2];
}
for (int k = 0; k < 36; k++) {
one[i][0] -= two[i][k][1];
one[i][1] -= two[i][k][3];
one[i][2] -= two[k][i][3];
one[i][3] -= two[k][i][4];
}
for (int l = 0; l < 36; l++) {
one[i][0] -= two[i][l][2];
one[i][1] -= two[i][l][4];
one[i][2] -= two[i][l][5];
one[i][3] -= two[l][i][5];
}
for (int j = 0; j < 36; j++) {
for (int k = 0; k < 36; k++) {
one[i][0] -= three[i][j][k][0];
one[i][1] -= three[j][i][k][0];
one[i][2] -= three[j][k][i][0];
one[i][3] -= three[j][k][i][1];
}
}
for (int j = 0; j < 36; j++) {
for (int l = 0; l < 36; l++) {
one[i][0] -= three[i][j][l][1];
one[i][1] -= three[j][i][l][1];
one[i][2] -= three[j][i][l][2];
one[i][3] -= three[j][l][i][2];
}
}
for (int k = 0; k < 36; k++) {
for (int l = 0; l < 36; l++) {
one[i][0] -= three[i][k][l][2];
one[i][1] -= three[i][k][l][3];
one[i][2] -= three[k][i][l][3];
one[i][3] -= three[k][l][i][3];
}
}
for (int j = 0; j < 36; j++) {
for (int k = 0; k < 36; k++) {
for (int l = 0; l < 36; l++) {
one[i][0] -= four[i][j][k][l];
one[i][1] -= four[j][i][k][l];
one[i][2] -= four[j][k][i][l];
one[i][3] -= four[j][k][l][i];
}
}
}
}
long long three_tot = 0, two_tot = 0, one_tot = 0, zero_tot = n * (n - 1) / 2;
for (int i = 0; i < 36; i++) {
for (int j = 0; j < 36; j++) {
for (int k = 0; k < 36; k++) {
three_tot += three[i][j][k][0];
three_tot += three[i][j][k][1];
three_tot += three[i][j][k][2];
three_tot += three[i][j][k][3];
}
}
}
for (int i = 0; i < 36; i++) {
for (int j = 0; j < 36; j++) {
two_tot += two[i][j][0];
two_tot += two[i][j][1];
two_tot += two[i][j][2];
two_tot += two[i][j][3];
two_tot += two[i][j][4];
two_tot += two[i][j][5];
}
}
for (int i = 0; i < 36; i++) {
one_tot += one[i][0];
one_tot += one[i][1];
one_tot += one[i][2];
one_tot += one[i][3];
}
zero_tot = zero_tot - one_tot - two_tot - three_tot;
if (d == 1) cout << three_tot << "\n";
if (d == 2) cout << two_tot << "\n";
if (d == 3) cout << one_tot << "\n";
if (d == 4) cout << zero_tot << "\n";
return 0;
}
Compilation message
pin.cpp: In function 'int CharToInt(char)':
pin.cpp:9:1: warning: control reaches end of non-void function [-Wreturn-type]
9 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
16828 KB |
Output is correct |
2 |
Correct |
44 ms |
16772 KB |
Output is correct |
3 |
Correct |
44 ms |
16668 KB |
Output is correct |
4 |
Correct |
46 ms |
17108 KB |
Output is correct |
5 |
Correct |
46 ms |
17120 KB |
Output is correct |
6 |
Correct |
47 ms |
17108 KB |
Output is correct |
7 |
Correct |
45 ms |
17068 KB |
Output is correct |
8 |
Correct |
49 ms |
17084 KB |
Output is correct |
9 |
Correct |
48 ms |
17104 KB |
Output is correct |
10 |
Correct |
59 ms |
17024 KB |
Output is correct |
11 |
Correct |
47 ms |
17112 KB |
Output is correct |
12 |
Correct |
47 ms |
17124 KB |
Output is correct |
13 |
Correct |
49 ms |
17008 KB |
Output is correct |
14 |
Correct |
46 ms |
17108 KB |
Output is correct |
15 |
Correct |
47 ms |
17108 KB |
Output is correct |
16 |
Correct |
60 ms |
28108 KB |
Output is correct |
17 |
Correct |
64 ms |
28120 KB |
Output is correct |
18 |
Correct |
63 ms |
28108 KB |
Output is correct |
19 |
Correct |
66 ms |
28072 KB |
Output is correct |
20 |
Correct |
66 ms |
28120 KB |
Output is correct |