#include <bits/stdc++.h>
#include <cstdio>
#include <map>
using namespace std;
#define _for(i, a, b) for (int i = (a); i < (int)(b); ++i)
typedef unsigned int uint;
typedef long long LL;
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 Item {
uint h[2];
void add(const Item &x, int len) {
for (int i = 0; i < 2; i++) {
h[i] = (h[i] * BP[i][len]) /*%Mod[i]*/ + x.h[i];
// h[i]%=Mod[i];
}
}
bool operator<(const Item &x) const {
_for(i, 0, 2) if (h[i] != x.h[i]) return h[i] < x.h[i];
return false;
}
};
Item f[NN][NN][LK + 4];
map<Item, int> cnt;
int main() {
scanf("%d %d %lld\n", &N, &M, &K);
for (int i = 0; i < N; i++)
scanf("%s", S[i]);
for (int di = 0; di < 8; di++) {
BP[0][0] = Base[0], BP[1][0] = Base[1];
for (int i = 1; i <= LK; i++) {
BP[0][i] = (BP[0][i - 1] * BP[0][i - 1]) /*%Mod[0]*/;
BP[1][i] = (BP[1][i - 1] * BP[1][i - 1]) /*%Mod[1]*/;
}
_for(i, 0, N) _for(j, 0, M) {
f[i][j][0].h[0] = S[i][j] - 'a' /*%Mod[0]*/;
f[i][j][0].h[1] = S[i][j] - 'a' /*%Mod[1]*/;
}
int dx = D[di][0], dy = D[di][1];
LL len = 1;
for (int l = 1; l <= LK; l++) {
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
f[i][j][l] = f[i][j][l - 1];
f[i][j][l].add(f[((i + dx * len) % N + N) % N]
[((j + dy * len) % M + M) % M][l - 1],
l - 1);
}
len *= 2;
}
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
len = K;
Item t;
int x = i, y = j;
t.h[0] = t.h[1] = 0;
for (int log = LK; log >= 0; log--)
if ((1 << log) & len) {
len -= (1 << log);
t.add(f[x][y][log], log);
x = ((x + dx * (1 << log)) % N + N) % N;
y = ((y + dy * (1 << log)) % M + M) % M;
}
cnt[t]++;
}
}
LL x = 0, y = 1LL * (N * M * 8) * (N * M * 8);
for (const auto p : cnt)
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:37:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d %d %lld\n", &N, &M, &K);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
osmosmjerka.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%s", S[i]);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
6 ms |
844 KB |
Output is correct |
6 |
Correct |
42 ms |
5060 KB |
Output is correct |
7 |
Correct |
552 ms |
32028 KB |
Output is correct |
8 |
Correct |
2039 ms |
82848 KB |
Output is correct |