#include <bits/stdc++.h>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Osmosmjerka"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 507
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
const int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};
const int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1};
int n, m, k;
ll po[maxn * maxn];
char a[maxn][maxn];
unordered_map<ll, ll> S;
int nex(int x, int y) {
if(x < 1) x = y;
if(x > y) x -= y;
return x;
}
bool inside(int i, int j) {
return 1 <= i && i <= n && 1 <= j && j <= m;
}
ll MulHash(ll a, ll p, ll k) {
ll ans = 0;
for(; k > 0; k >>= 1, a = (a * p + a) % mod, p = (p * p) % mod) {
if(k & 1) ans = (ans * p + a) % mod;
}
return ans;
}
ll HASH(string s) {
ll ans = 0;
for(char c : s) ans = (ans * base + c) % mod;
return ans;
}
void solve()
{
cin >> n >> m >> k;
fi(i, 1, n) fi(j, 1, m) cin >> a[i][j];
fi(i, 1, n) fi(j, 1, m) fi(t, 0, 7) {
ll Hash = 0, remain = 0, len = 0;
int u = i, v = j;
do {
++len;
Hash = (Hash * base + a[u][v]) % mod;
u = nex(u + dx[t], n), v = nex(v + dy[t], m);
} while(!(u == i && v == j));
for(int u = i, v = j, _ = 1; _ <= k % len; ++_) {
remain = (remain * base + a[u][v]) % mod;
u = nex(u + dx[t], n), v = nex(v + dy[t], m);
}
Hash = MulHash(Hash, po[len], k / len);
Hash = (Hash * po[k % len] + remain) % mod;
S[Hash] ++;
}
ll mau = 1ll * m * n * 8;
mau = mau * mau;
ll tu = 0;
for(auto [x, sl] : S) {
tu = 1ll * tu + sl * sl;
}
ll g = __gcd(tu, mau);
tu /= g, mau /= g;
cout << tu << '/' << mau;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
po[0] = 1;
fi(i, 1, maxn * maxn - 2) po[i] = po[i - 1] * base % mod;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
osmosmjerka.cpp: In function 'int main()':
osmosmjerka.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
osmosmjerka.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
2396 KB |
Output is correct |
5 |
Correct |
6 ms |
2520 KB |
Output is correct |
6 |
Correct |
2126 ms |
4720 KB |
Output is correct |
7 |
Execution timed out |
4032 ms |
3264 KB |
Time limit exceeded |
8 |
Execution timed out |
4075 ms |
22068 KB |
Time limit exceeded |