#include <bits/stdc++.h>
using namespace std;
#define _for(i, a, b) for (int i = (a); i < (int)(b); ++i)
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
const int D[][2] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0},
{1, -1}, {-1, 1}, {1, 1}, {-1, -1}},
NN = 500 + 4, LK = 21;
const uint Base[2] = {1183531, 1041221};
inline LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); }
int N, M;
LL K;
char S[NN][NN];
uint BP[2][LK + 4]; // BP[i,l]: Base[i]^(2^l)
struct HashItem {
uint h[2];
HashItem() { h[0] = h[1] = 0; }
void add(const HashItem &x, int len) {
_for(i, 0, 2) h[i] = (h[i] * BP[i][len]) + x.h[i];
}
ULL val() { return ((ULL)h[0] << 32) + h[1]; }
};
HashItem H[NN][NN][LK + 4];
unordered_map<ULL, int> HC;
int main() {
scanf("%d %d %lld\n", &N, &M, &K);
for (int i = 0; i < N; i++)
scanf("%s", S[i]);
BP[0][0] = Base[0], BP[1][0] = Base[1];
int lk = ceil(log2(K)) + 1;
for (int l = 1; l <= lk; l++)
_for(i, 0, 2) BP[i][l] = (BP[i][l - 1] * BP[i][l - 1]);
_for(i, 0, N) _for(j, 0, M) H[i][j][0].h[0] = H[i][j][0].h[1] = S[i][j] - 'a';
_for(di, 0, 8) {
int dx = D[di][0], dy = D[di][1];
LL len = 1;
for (int l = 1; l <= lk; l++) {
_for(x, 0, N) _for(y, 0, M) {
int nx = ((x + dx * len) % N + N) % N,
ny = ((y + dy * len) % M + M) % M;
(H[x][y][l] = H[x][y][l - 1]).add(H[nx][ny][l - 1], l - 1);
}
len *= 2;
}
_for(x, 0, N) _for(y, 0, M) {
len = K;
HashItem h;
for (int l = lk, nx = x, ny = y; l >= 0; l--)
if ((1 << l) & len) {
len -= (1 << l), h.add(H[nx][ny][l], l);
nx = ((nx + dx * (1 << l)) % N + N) % N;
ny = ((ny + dy * (1 << l)) % M + M) % M;
}
HC[h.val()]++;
}
}
LL x = 0, y = 1LL * (N * M * 8) * (N * M * 8);
for (auto p : HC)
x += 1LL * (p.second) * (p.second);
LL g = gcd(x, y);
printf("%lld/%lld\n", x / g, y / g);
return 0;
}
Compilation message
osmosmjerka.cpp: In function 'int main()':
osmosmjerka.cpp:28:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d %d %lld\n", &N, &M, &K);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
osmosmjerka.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%s", S[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
49864 KB |
Output is correct |
2 |
Correct |
25 ms |
49996 KB |
Output is correct |
3 |
Correct |
24 ms |
49924 KB |
Output is correct |
4 |
Correct |
26 ms |
49980 KB |
Output is correct |
5 |
Correct |
30 ms |
50116 KB |
Output is correct |
6 |
Incorrect |
65 ms |
52696 KB |
Output isn't correct |
7 |
Incorrect |
634 ms |
69948 KB |
Output isn't correct |
8 |
Incorrect |
2936 ms |
130524 KB |
Output isn't correct |