#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 HashVal {
uint h[2];
HashVal() { h[0] = h[1] = 0; }
inline void append(const HashVal &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]; }
};
HashVal H[NN][NN][LK + 4];
unordered_map<ULL, int> HC;
int mod(int x, int md) { return ((x % md) % md + md) % md; }
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];
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) {
(H[x][y][l] = H[x][y][l - 1])
.append(H[mod(x + dx * len, N)][mod(y + dy * len, M)][l - 1],
l - 1);
}
len *= 2;
}
_for(x, 0, N) _for(y, 0, M) {
len = K;
HashVal h;
for (int l = LK, nx = x, ny = y; l >= 0; l--)
if ((1 << l) & len) {
len -= (1 << l), h.append(H[nx][ny][l], l);
nx = mod(nx + dx * (1 << l), N), ny = mod(ny + dy * (1 << l), 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:29:36: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | for (int i = 0; i < N; i++) scanf("%s", S[i]);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
50156 KB |
Output is correct |
2 |
Correct |
9 ms |
50000 KB |
Output is correct |
3 |
Correct |
8 ms |
50000 KB |
Output is correct |
4 |
Correct |
9 ms |
50000 KB |
Output is correct |
5 |
Correct |
11 ms |
50256 KB |
Output is correct |
6 |
Correct |
31 ms |
52396 KB |
Output is correct |
7 |
Correct |
195 ms |
62324 KB |
Output is correct |
8 |
Correct |
865 ms |
72496 KB |
Output is correct |