1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
#ifdef ONLINE_JUDGE #define TEST 1 #else
#define LOCAL 1 #endif #ifndef LOCAL #define TEST 1 #endif #include <bits/stdc++.h> #define int int64_t #ifdef TEST #pragma GCC optimize(3, "Ofast", "inline") #define endl '\n' #endif #ifdef LOCAL #define endl " **\n" #endif #define tomax(x, y) x = max(x, (y)) #define tomin(x, y) x = min(x, (y)) using namespace std; #ifdef TEST const int MAX = 4005; #else #ifdef LOCAL const int MAX = 105; #endif #endif const int INF = 0x3f3f3f3f3f3f3f3fll; const int MOD = 1000000007; int call_count = 0;
#define HASH_DOUBLE
#define HASH_REV
#ifdef HASH_NETURAL #define HASH_CODE uint64_t #else #define HASH_CODE int64_t #endif
const int HX1 = 157, HY1 = 13331, HM1 = 1000000009; #ifdef HASH_DOUBLE const int HX2 = 131, HY2 = 10037, HM2 = 999999937; #endif
HASH_CODE px1[MAX]; HASH_CODE py1[MAX]; HASH_CODE h1[MAX][MAX]; #ifdef HASH_DOUBLE HASH_CODE px2[MAX]; HASH_CODE py2[MAX]; HASH_CODE h2[MAX][MAX]; #endif #ifdef HASH_REV HASH_CODE g1[MAX][MAX]; #ifdef HASH_DOUBLE HASH_CODE g2[MAX][MAX]; #endif #endif
#ifdef HASH_NETURAL #define mm0(x, m) (x) #define mm1(x, m) (x) #define mm2(x, m) (x) #else #define mm0(x, m) (((x) % (m) + (m)) % (m)) #define mm1(x, m) (((x) + (m)) % (m)) #define mm2(x, m) ((x) % (m)) #endif
void hash_init(int n) { px1[0] = 1, py1[0] = 1; #ifdef HASH_DOUBLE px2[0] = 1, py2[0] = 1; #endif for (int i = 0; i < n; i++) { px1[i + 1] = mm2(px1[i] * HX1, HM1); py1[i + 1] = mm2(py1[i] * HY1, HM1); #ifdef HASH_DOUBLE px2[i + 1] = mm2(px2[i] * HX2, HM2); py2[i + 1] = mm2(py2[i] * HY2, HM2); #endif } }
void hash_it(const char s[MAX][MAX], int n, int m) { for (int i = 0; i <= n; i++) { h1[i][0] = 0; #ifdef HASH_DOUBLE h2[i][0] = 0; #endif } for (int j = 0; j <= m; j++) { h1[0][j] = 0; #ifdef HASH_DOUBLE h2[0][j] = 0; #endif } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { h1[i + 1][j + 1] = mm0((h1[i][j + 1] * HX1 + h1[i + 1][j] * HY1 + s[i][j] - mm2(h1[i][j] * HX1, HM1) * HY1), HM1); #ifdef HASH_DOUBLE h2[i + 1][j + 1] = mm0((h2[i][j + 1] * HX2 + h2[i + 1][j] * HY2 + s[i][j] - mm2(h2[i][j] * HX2, HM2) * HY2), HM2); #endif } }
#ifdef HASH_REV for (int i = n + 1; i >= 1; i--) { g1[i][m + 1] = 0; #ifdef HASH_DOUBLE g2[i][m + 1] = 0; #endif } for (int j = m + 1; j >= 1; j--) { g1[n + 1][j] = 0; #ifdef HASH_DOUBLE g2[n + 1][j] = 0; #endif } for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { g1[i + 1][j + 1] = mm0((g1[i + 2][j + 1] * HX1 + g1[i + 1][j + 2] * HY1 + s[i][j] - mm2(g1[i + 2][j + 2] * HX1, HM1) * HY1), HM1); #ifdef HASH_DOUBLE g2[i + 1][j + 1] = mm0((g2[i + 2][j + 1] * HX2 + g2[i + 1][j + 2] * HY2 + s[i][j] - mm2(g2[i + 2][j + 2] * HX2, HM2) * HY2), HM2); #endif } } #endif }
inline int hash_sub(int x, int y, int xx, int yy, bool reverse = false) { if (x > xx) swap(x, xx); if (y > yy) swap(y, yy); #ifdef HASH_REV if (reverse) { x++, y++, xx += 2, yy += 2; int s1 = mm0((g1[x][y] - g1[xx][y] * px1[xx - x] - g1[x][yy] * py1[yy - y] + mm2(g1[xx][yy] * px1[xx - x], HM1) * py1[yy - y]), HM1); #ifdef HASH_DOUBLE int s2 = mm0((g2[x][y] - g2[xx][y] * px2[xx - x] - g2[x][yy] * py2[yy - y] + mm2(g2[xx][yy] * px2[xx - x], HM2) * py2[yy - y]), HM2); return s1 * HM2 + s2; #else return s1; #endif } #endif xx++, yy++; int s1 = mm0((h1[xx][yy] - h1[x][yy] * px1[xx - x] - h1[xx][y] * py1[yy - y] + mm2(h1[x][y] * px1[xx - x], HM1) * py1[yy - y]), HM1); #ifdef HASH_DOUBLE int s2 = mm0((h2[xx][yy] - h2[x][yy] * px2[xx - x] - h2[xx][y] * py2[yy - y] + mm2(h2[x][y] * px2[xx - x], HM2) * py2[yy - y]), HM2); return s1 * HM2 + s2; #else return s1; #endif }
#ifdef HASH_REV inline bool is_central_symmetry(int x, int y, int xx, int yy) { return hash_sub(x, y, xx, yy) == hash_sub(x, y, xx, yy, true); } #endif
char a[MAX][MAX]; char b[MAX][MAX]; int p[MAX][MAX];
void zoom(int n, int m) { for (int i = 0; i <= 2 * n; i++) { for (int j = 0; j <= 2 * m; j++) { b[i][j] = '#'; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { b[2 * i + 1][2 * j + 1] = a[i][j]; } } }
inline bool check(int x, int y, int r) { return is_central_symmetry(x - r, y - r, x + r, y + r); }
void manacher(const char s[MAX][MAX], int n, int m) { for (int i = 0; i < n; i++) { int right_radius = -1, right_center = -1; for (int j = 0; j < m; j++) { int r = 1; if (j < right_radius) if (p[i][2 * right_center - j] < right_radius - j) r = p[i][2 * right_center - j]; else r = right_radius - j; else r = 1; while (0 <= j - r and j + r < m and 0 <= i - r and i + r < n) if (check(i, j, r)) r++; else break; p[i][j] = r - 1; if (j + r > right_radius) { right_radius = j + r; right_center = j; } } } }
inline int largest_central_symmetry_square(int x, int y, bool odd = true) { return p[x * 2 + 1 + (int)(not odd)][y * 2 + 1 + (int)(not odd)]; }
void solve() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } zoom(n, m); n = n << 1 | 1, m = m << 1 | 1; hash_it(b, n, m); manacher(b, n, m); int res = 0; n >>= 1, m >>= 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { res += (largest_central_symmetry_square(i, j) + 1) / 2; res += largest_central_symmetry_square(i, j, false) / 2; } } cout << res << endl; }
int32_t main() { #ifdef TEST cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); #endif #ifdef LOCAL freopen("in.txt", "r", stdin); clock_t start_time = clock(); #endif cout << fixed << setprecision(2);
hash_init(MAX - 1); int t = 1; while (t--) solve();
#ifdef LOCAL cout << "Used " << call_count << " Function Calls." << endl; cout << "Used " << (int)(clock() - start_time) << " Microseconds of Time." << endl; #endif return 0; }
|