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
| #pragma GCC optimize("Ofast") #include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int N = 2000 + 10, Log = 20, inf = 0x3f3f3f3f; const int mod[2] = {1000000007, 1000000009}, base[2] = {131, 251};
#define gc()(is==it?it=(is=in)+fread(in,1,Q,stdin),(is==it?EOF:*is++):*is++) const int Q=(1<<24)+1; char in[Q],*is=in,*it=in,c; void read(long long &n){ for(n=0;(c=gc())<'0'||c>'9';); for(;c<='9'&&c>='0';c=gc())n=n*10+c-48; }
void read(int &n){ for(n=0;(c=gc())<'0'||c>'9';); for(;c<='9'&&c>='0';c=gc())n=n*10+c-48; }
void read(char &n){ for(;(c=gc())<'a'||c>'z';); n = c; }
pll operator*(const pll &p1, const pll &p2) { return {p1.first * p2.first % mod[0], p1.second * p2.second % mod[1]}; } pll operator+(const pll &p1, const pll &p2) { return {(p1.first + p2.first) % mod[0], (p1.second + p2.second) % mod[1]}; } pll operator-(const pll &p1, const pll &p2) { return {(p1.first - p2.first + mod[0]) % mod[0], (p1.second - p2.second + mod[1]) % mod[1]}; }
ll hs[N][N][2], sh[N][N][2], pw[N * N][2]; char ch[N][N], re[N][N]; int n, m;
void RE(int &x, int &y){ x = n - x + 1; y = m - y + 1; }
ll geths(int lx, int ly, int rx, int ry, int p){ ll res = hs[rx][ry][p] - hs[lx - 1][ry][p]; if(res < 0) res += mod[p]; res -= hs[rx][ly - 1][p]; if(res < 0) res += mod[p]; res += hs[lx - 1][ly - 1][p]; if(res >= mod[p]) res -= mod[p]; return res; }
pll geths(int lx, int ly, int rx, int ry){ ll a = geths(lx, ly, rx, ry, 0); ll b = geths(lx, ly, rx, ry, 1); return {a, b}; }
ll getsh(int lx, int ly, int rx, int ry, int p){ ll res = sh[rx][ry][p] - sh[lx - 1][ry][p]; if(res < 0) res += mod[p]; res -= sh[rx][ly - 1][p]; if(res < 0) res += mod[p]; res += sh[lx - 1][ly - 1][p]; if(res >= mod[p]) res -= mod[p]; return res; }
pll getsh(int lx, int ly, int rx, int ry){ ll a = getsh(lx, ly, rx, ry, 0); ll b = getsh(lx, ly, rx, ry, 1); return {a, b}; }
bool calc(int lx, int ly, int rx, int ry){ pll a = geths(lx, ly, rx, ry); int k = (lx - 1) * m + ly; RE(lx, ly);RE(rx, ry); swap(lx, rx);swap(ly, ry); k = (lx - 1) * m + ly - k; pll b = getsh(lx, ly, rx, ry); if(k >= 0){ a = a * pll{pw[k][0], pw[k][1]}; }else{ b = b * pll{pw[-k][0], pw[-k][1]}; } return a == b; }
void updhs(int i, int j, int a, int p){ hs[i][j][p] = hs[i - 1][j][p] + hs[i][j - 1][p]; if(hs[i][j][p] >= mod[p]) hs[i][j][p] -= mod[p]; hs[i][j][p] -= hs[i - 1][j - 1][p]; if(hs[i][j][p] < 0) hs[i][j][p] += mod[p]; hs[i][j][p] += pw[(i - 1) * m + j][p] * a % mod[p]; if(hs[i][j][p] >= mod[p]) hs[i][j][p] -= mod[p]; }
void updsh(int i, int j, int a, int p){ sh[i][j][p] = sh[i - 1][j][p] + sh[i][j - 1][p]; if(sh[i][j][p] >= mod[p]) sh[i][j][p] -= mod[p]; sh[i][j][p] -= sh[i - 1][j - 1][p]; if(sh[i][j][p] < 0) sh[i][j][p] += mod[p]; sh[i][j][p] += pw[(i - 1) * m + j][p] * a % mod[p]; if(sh[i][j][p] >= mod[p]) sh[i][j][p] -= mod[p]; }
void solve() { cin >> n >> m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cin >> ch[i][j]; re[n - i + 1][m - j + 1] = ch[i][j]; int a = ch[i][j] - 'a' + 1; updhs(i, j, a, 0); updhs(i, j, a, 1); } } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ int a = re[i][j] - 'a' + 1; updsh(i, j, a, 0); updsh(i, j, a, 1); } } ll ans = 0; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ int l = 1, r = min({i, j, n - i + 1, m - j + 1}); while(l <= r){ int mid = (l + r) >> 1; if(calc(i - mid + 1, j - mid + 1, i + mid - 1, j + mid - 1)){ l = mid + 1; }else{ r = mid - 1; } } ans += r; l = 1, r = min({i, j, n - i, m - j}); while(l <= r){ int mid = (l + r) >> 1; if(calc(i - mid + 1, j - mid + 1, i + mid, j + mid)){ l = mid + 1; }else{ r = mid - 1; } } ans += r; } } cout << ans << '\n'; }
int main() { int T = 1; ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); pw[0][0] = pw[0][1] = 1; for(int i = 1; i <= 4000000; i++){ pw[i][0] = pw[i - 1][0] * base[0] % mod[0]; pw[i][1] = pw[i - 1][1] * base[1] % mod[1]; } while (T--) solve(); return 0; }
|