求优化
查看原帖
求优化
546830
XSean楼主2022/11/30 21:27

在场上想的暴力做法

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

const int N = 1010, mod = 998244353;
int t, id;
int n, m;
int c, f;
char g[N][N];
ULL totc, totf;
struct Node{
    int /*up,*/ down, right;
}nodes[N][N];


void check(int x, int y){
    /*up =*/nodes[x][y].down = nodes[x][y].right = 0;
    for(int i = x + 1; i <= n; i++){
        if(g[i][y] == '0') nodes[x][y].down++;
        else break;
    }
    for(int i = y + 1; i <= m; i++){
        if(g[x][i] == '0') nodes[x][y].right++;
        else break;
    }
    nodes[x][y].right << endl;
}

ULL findc(int x, int y){
    ULL totc = 0;
    int upr = nodes[x][y].right;
    int l = nodes[x][y].down;
    for(int i = x + 2; i <= x + l; i++){
        totc += (ULL)upr * nodes[i][y].right;
    }
    return totc;
}

ULL findf(int x, int y){
    ULL totf = 0;
    int upr = nodes[x][y].right;
    int l = nodes[x][y].down;
    for(int i = x + 2; i <= x + l; i++){
        totf += (ULL)upr * nodes[i][y].right * nodes[i][y].down;
    }
    return totf;
}
int main(){
    //freopen("plant.in", "r", stdin);
    //freopen("plant.out", "w", stdout);
    cin >> t >> id;
    for(int k = 1; k <= t; k++){
        cin >> n >> m >> c >> f;
        if(c == 0 && f == 0){
            cout << 0 << " " << 0 << endl;
            continue;
        }
        totc = 0, totf = 0;
        memset(g, 0, sizeof(g));
        memset(nodes, 0, sizeof(nodes));
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                cin >> g[i][j];
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(g[i][j] == '0') check(i, j);
            }
        }
        for(int i = 1; i < n; i++){
            for(int j = 1; j < m; j++){
                if(c) totc += findc(i, j);
                if(f) totf += findf(i, j);
            }
        }
        cout << (totc % mod) * (c % mod) % mod << " " << (totf % mod) * (f % mod) % mod << endl;
    }

    return 0;
}

2022/11/30 21:27
加载中...