1 条题解

  • 0
    @ 2025-7-27 17:44:45

    O(nm)O(n*m) 枚举

    符合条件的格子为:每条斜线上的格子,并且数值相同。

    所以只要求出每条斜线上相同的点的个数,在求和就能求出结果。

    其中主对角线方向和副对角线方向要分别统计。

    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    
    #define x first
    #define y second
    
    const int N = 1e3 + 10, mod = 998244353;
    
    
    int a[N * 2][N], b[N * 2][N];
    
    LL cal(int m[])
    {
        LL ans = 0;
        for(int j = 1; j <= 1000; j ++)
          for(auto i : m[j])
            ans += i.y * (i.y - 1);
        
        return ans;
    }
    
    void solve()
    {
        int n, m;
        cin >> n >> m;
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
            {
                int x;
                cin >> x;
                a[x][i + j] ++;
                b[x][i - j + 1000] ++;
            }
            
        LL ans = cal(a) + cal(b);
        cout << ans << endl;
    }
    
    int main(){
        int t = 1;
        // cin >> t;
        while(t --)
            solve();
    }
    
    • 1

    信息

    ID
    431
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    递交数
    11
    已通过
    6
    上传者