package main
import (
"fmt"
)
const MAXN int = 1001
const baser int = 491
const basec int = 499
var powr [MAXN]int64
var powc [MAXN]int64
var arr1 [MAXN][MAXN]int
var arr2 [MAXN][MAXN]int
var arr3 [MAXN][MAXN]int
var sum1 [MAXN][MAXN]int64
var sum2 [MAXN][MAXN]int64
var sum3 [MAXN][MAXN]int64
var n int
var m int
func init() {
powr[0] = 1
powc[0] = 1
for i := 1; i < MAXN; i++ {
powr[i] = powr[i-1] * int64(baser)
}
for i := 1; i < MAXN; i++ {
powc[i] = powc[i-1] * int64(basec)
}
}
func buildHash(arr *[MAXN][MAXN]int, sum *[MAXN][MAXN]int64) {
for i := 1; i <= n; i++ {
for j := 1; j <= m; j++ {
sum[i][j] = sum[i][j-1]*int64(basec) + int64(arr[i][j])
}
}
for i := 1; i <= n; i++ {
for j := 1; j <= m; j++ {
sum[i][j] += sum[i-1][j] * int64(baser)
}
}
}
func hash(sum *[MAXN][MAXN]int64, a int, b int, c int, d int) int64 {
ans := sum[c][d] - sum[a-1][d]*powr[c-a+1] - sum[c][b-1]*powc[d-b+1] + sum[a-1][b-1]*powr[d-b+1]*powc[c-a+1]
return ans
}
func number() int {
ans := 0
// 奇数长度,实点做中心
for i := 1; i <= n; i++ {
for j := 1; j <= m; j++ {
l := 1
r := min(min(i, n-i+1), min(j, m-j+1))
find := 1
var m int
for l <= r {
m = (l + r) / 2
if ok(i-m+1, j-m+1, i+m-1, j+m-1) {
find = m
l = m + 1
} else {
r = m - 1
}
}
ans += find
}
}
// 偶数长度
// 虚点做中心
for i := 1; i < n; i++ {
for j := 1; j < m; j++ {
// 左上角点为代表
l := 1
r := min(min(i, j), min(n-i, m-j))
find := 0
var m int
for l <= r {
m = (l + r) / 2
if ok(i-m+1, j-m+1, i+m, j+m) {
find = m
l = m + 1
} else {
r = m - 1
}
}
ans += find
}
}
return ans
}
func ok(a int, b int, c int, d int) bool {
if a == c {
return true
}
h1 := hash(&sum1, a, b, c, d)
h2 := hash(&sum2, n-c+1, b, n-a+1, d)
h3 := hash(&sum3, a, m-d+1, c, m-b+1)
return h1 == h2 && h1 == h3
}
func min(x int, y int) int {
if x < y {
return x
}
return y
}
func main() {
inputs := []int{5, 5,
4, 2, 4, 4, 4,
3, 1, 4, 4, 3,
3, 5, 3, 3, 3,
3, 1, 5, 3, 3,
4, 2, 1, 2, 4}
ii := 0
n = inputs[ii]
ii++
m = inputs[ii]
ii++
for i := 1; i <= n; i++ {
for j := 1; j <= m; j++ {
arr1[i][j] = inputs[ii]
ii++
arr2[n-i+1][j] = arr1[i][j]
arr3[i][m-j+1] = arr1[i][j]
}
}
buildHash(&arr1, &sum1)
buildHash(&arr2, &sum2)
buildHash(&arr3, &sum3)
fmt.Println(number())
}
评论