#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"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#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];
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), p = (p * p) ) {
if(k & 1) ans = (ans * p + a);
}
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]) ;
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]) ;
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) ;
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 ;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
osmosmjerka.cpp: In function 'int main()':
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".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
osmosmjerka.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
4 ms |
2652 KB |
Output is correct |
6 |
Correct |
1057 ms |
5168 KB |
Output is correct |
7 |
Execution timed out |
4065 ms |
4804 KB |
Time limit exceeded |
8 |
Correct |
3445 ms |
35328 KB |
Output is correct |