正解:DP+前缀和
做这种题一定要画图!!!
代码:
#include<iostream>
#include<cstdio>
using namespace std;
long long f[2001][2001],a[2001][2001],d[5000001],n,ans;
int main()
{
freopen("farmer.in","r",stdin);
freopen("farmer.out","w",stdout);
scanf("%lld",&n);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
scanf("%lld",&a[i][j]);
f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
for(int x=1; x<=i; x++)
for(int y=1; y<=j; y++)
d[f[i][j]-f[i][y-1]-f[x-1][j]+f[x-1][y-1]]++;
for(int x=i+1; x<=n; x++)
for(int y=j+1; y<=n; y++)
ans+=d[f[x][y]-f[x][j]-f[i][y]+f[i][j]];
for(int x=1; x<=i; x++)
for(int y=1; y<=j; y++)
d[f[i][j]-f[i][y-1]-f[x-1][j]+f[x-1][y-1]]=0;
for(int x=1; x<=i; x++)
for(int y=j+1; y<=n; y++)
d[f[i][y]-f[x-1][y]-f[i][j]+f[x-1][j]]++;
for(int x=i+1; x<=n; x++)
for(int y=1; y<=j; y++)
ans+=d[f[x][j]-f[i][j]-f[x][y-1]+f[i][y-1]];
for(int x=1; x<=i; x++)
for(int y=j+1; y<=n; y++)
d[f[i][y]-f[x-1][y]-f[i][j]+f[x-1][j]]=0;
}
printf("%lld",ans);
return 0;
}