源代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
const int k=998244353;
bool o[2200][2200];
int a[2200][2200];
int b[2200][2200];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t,id;
cin>>t>>id;
for(int p=1;p<=t;p++){
memset(o,0,sizeof(o));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int c1,f1;
int sum=0;
cin>>n>>m>>c1>>f1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char c;
cin>>c;
o[i][j]=(int)(c-'0');
}
}
for(int i=n;i>=1;i--){
for(int j=m;j>=1;j--){
if(o[i][j]==0){
a[i][j]=a[i][j+1]+1;
}
}
}
for(int j=m;j>=1;j--){
for(int i=n;i>=1;i--){
if(o[i][j]==0){
b[i][j]=b[i+1][j]+1;
}
}
}
int c=0,f=0;
for(int i=1;i<=n-1;i++){
for(int j=1;j<=m-1;j++){
if(a[i][j]>1&&o[i+1][j]==0){
for(int i1=i+2;i1<=n;i1++){
if(o[i1][j]==1){
break;
}
if(a[i1][j]>1){
c+=(a[i][j]-1)*(a[i1][j]-1);
}
if(b[i1][j]>=2){
f+=(a[i][j]-1)*(a[i1][j]-1)*(b[i1][j]-1);
}
}
}
}
}
cout<<c*c1%k<<" "<<f*f1%k<<endl;
}
}
两个点tle了
求调QWQ