差分
代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 1999999999
int n,m,sum,a[][],b[][],c[][];
int main(){
int minn=INF;
cin>>n>>m;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
cin>>a[i][j],sum+=a[i][j];
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
if (sum%(i*j)==)
{
memset(b,,sizeof(b)); memset(c,,sizeof(c));
int num=;
for (int i1=;i1<=n;i1++)
for (int j1=;j1<=m;j1++)
{
c[i1][j1]=c[i1-][j1]+c[i1][j1-]-c[i1-][j1-]+b[i1][j1];
if (a[i1][j1]<c[i1][j1])
{
goto ll;
} else
if (a[i1][j1]==c[i1][j1])
{
continue;
}
{
if (i+i1->n || j+j1->m)
{
goto ll;
}
int tmp=a[i1][j1]-c[i1][j1];
b[i1+i][j1]-=tmp;
b[i1][j1+j]-=tmp;
b[i1+i][j1+j]+=tmp;
c[i1][j1]+=tmp;
num+=tmp;
}
}
minn=min(minn,num);
ll:;
}
cout<<minn;
}