# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
482425 | chenwz | Osmosmjerka (COCI17_osmosmjerka) | C++14 | 2039 ms | 82848 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |