#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MX = 2005;
const ll mod = 420691273;
int n, m;
string G[MX]; int grd[MX][MX];
ll p[MX][MX], pref[MX][MX]; // 2d hashing
ll query(int x, int y, int sx, int sy){
ll get = pref[x + sx][y + sy] + pref[x][y];
(get += (mod - pref[x + sx][y]) + (mod - pref[x][y + sy])) %= mod;
if(get < 0) get += mod;
return get;
}
bool check_eq(int ax, int ay, int bx, int by, int sx, int sy){
ll geta = (query(ax, ay, sx, sy) * 1ll * p[bx][by]) % mod;
ll getb = (query(bx, by, sx, sy) * 1ll * p[ax][ay]) % mod;
if(geta < 0) geta += mod;
if(getb < 0) getb += mod;
if(geta == getb) return true;
return false;
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> m;
for(int i = 0; i < n; i ++){
cin >> G[i];
for(int j = 0; j < m; j ++)
grd[i][j] = (G[i][j] == '*' ? 1 : 0);
}
for(int i = 0; i < MX; i ++){
for(int j = 0; j < MX; j ++){
if(i == 0){
if(j == 0) p[i][j] = 1ll;
else p[i][j] = (p[i][j - 1] * 3ll) % mod;
}else p[i][j] = (p[i - 1][j] * 2ll) % mod;
if(p[i][j] < 0) p[i][j] += mod;
}
}
for(int i = 0; i < 2 * n; i ++)
for(int j = 0; j < 2 * m; j ++){
pref[i + 1][j + 1] = ((grd[i % n][j % m] * 1ll * p[i][j]) % mod
+ pref[i + 1][j] + pref[i][j + 1] + mod - pref[i][j]) % mod;
if(pref[i + 1][j + 1] < 0) pref[i + 1][j + 1] += mod;
}
int ansx = 0, ansy = 0;
for(int x = 0; x < n; x ++){
for(int y = 0; y < m; y ++){
int lf = 0, rg = n - 1, dx = 0;
for(int md; lf <= rg;){
md = (lf + rg) / 2;
if(check_eq(ansx, ansy, x, y, md, m)){
dx = md;
lf = md + 1;
}else rg = md - 1;
}
lf = 0, rg = m - 1; int dy = 0;
for(int md; lf <= rg;){
md = (lf + rg) / 2;
if(check_eq(ansx + dx, ansy, x + dx, y, 1, md)){
dy = md; lf = md + 1;
}else rg = md - 1;
}
if(G[(x + dx) % n][(y + dy) % m] == '*' && G[(ansx + dx) % n][(ansy + dy) % m] == '.'){
ansx = x; ansy = y;
}
}
}
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++)
cout << G[(ansx + i) % n][(ansy + j) % m];
cout << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
32452 KB |
Output is correct |
2 |
Correct |
25 ms |
32368 KB |
Output is correct |
3 |
Correct |
20 ms |
32452 KB |
Output is correct |
4 |
Correct |
20 ms |
32460 KB |
Output is correct |
5 |
Correct |
20 ms |
32452 KB |
Output is correct |
6 |
Correct |
19 ms |
32460 KB |
Output is correct |
7 |
Correct |
18 ms |
32460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
32452 KB |
Output is correct |
2 |
Correct |
25 ms |
32368 KB |
Output is correct |
3 |
Correct |
20 ms |
32452 KB |
Output is correct |
4 |
Correct |
20 ms |
32460 KB |
Output is correct |
5 |
Correct |
20 ms |
32452 KB |
Output is correct |
6 |
Correct |
19 ms |
32460 KB |
Output is correct |
7 |
Correct |
18 ms |
32460 KB |
Output is correct |
8 |
Correct |
52 ms |
38548 KB |
Output is correct |
9 |
Correct |
20 ms |
32044 KB |
Output is correct |
10 |
Correct |
19 ms |
35460 KB |
Output is correct |
11 |
Correct |
52 ms |
38572 KB |
Output is correct |
12 |
Correct |
55 ms |
38768 KB |
Output is correct |
13 |
Correct |
53 ms |
38796 KB |
Output is correct |
14 |
Correct |
56 ms |
38816 KB |
Output is correct |
15 |
Correct |
54 ms |
38852 KB |
Output is correct |
16 |
Correct |
56 ms |
38860 KB |
Output is correct |
17 |
Correct |
59 ms |
38820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
32452 KB |
Output is correct |
2 |
Correct |
25 ms |
32368 KB |
Output is correct |
3 |
Correct |
20 ms |
32452 KB |
Output is correct |
4 |
Correct |
20 ms |
32460 KB |
Output is correct |
5 |
Correct |
20 ms |
32452 KB |
Output is correct |
6 |
Correct |
19 ms |
32460 KB |
Output is correct |
7 |
Correct |
18 ms |
32460 KB |
Output is correct |
8 |
Correct |
52 ms |
38548 KB |
Output is correct |
9 |
Correct |
20 ms |
32044 KB |
Output is correct |
10 |
Correct |
19 ms |
35460 KB |
Output is correct |
11 |
Correct |
52 ms |
38572 KB |
Output is correct |
12 |
Correct |
55 ms |
38768 KB |
Output is correct |
13 |
Correct |
53 ms |
38796 KB |
Output is correct |
14 |
Correct |
56 ms |
38816 KB |
Output is correct |
15 |
Correct |
54 ms |
38852 KB |
Output is correct |
16 |
Correct |
56 ms |
38860 KB |
Output is correct |
17 |
Correct |
59 ms |
38820 KB |
Output is correct |
18 |
Correct |
452 ms |
74052 KB |
Output is correct |
19 |
Correct |
28 ms |
44228 KB |
Output is correct |
20 |
Correct |
25 ms |
32612 KB |
Output is correct |
21 |
Correct |
429 ms |
74016 KB |
Output is correct |
22 |
Correct |
461 ms |
74052 KB |
Output is correct |
23 |
Correct |
424 ms |
74124 KB |
Output is correct |
24 |
Correct |
491 ms |
74220 KB |
Output is correct |
25 |
Correct |
442 ms |
74084 KB |
Output is correct |
26 |
Correct |
460 ms |
74056 KB |
Output is correct |
27 |
Correct |
455 ms |
73968 KB |
Output is correct |