• Day7


    Chiaki has an n × m matrix A. Rows are numbered from 1 to n from top to bottom and columns are numbered from 1 to m from left to right. The element in the i-th row and the j-th column is Aij.

    Let M({i1, i2, ..., is}, {j1, j2, ..., jt}) be the matrix that results from deleting row i1, i2, ..., is and column j1, j2, ..., jt of A and f({i1, i2, ..., is}, {j1, j2, ..., jt}) be the number of saddle points in matrix M({i1, i2, ..., is}, {j1, j2, ..., jt}).

    Chiaki would like to find all the value of f({i1, i2, ..., is}, {j1, j2, ..., jt}). As the output may be very large ((2n - 1)(2m - 1) matrix in total), she is only interested in the value

    $$left(sum_{egin{array}{r} 1 le i_1 < dots < i_s le n \ 1 le j_1 < dots < j_t le m \ 0 le s < n \ 0 le t < mend{array}} f({i_1,i_2,dots,i_s},{j_1,j_2,dots,j_t})
ight) mod (10^9+7).$$

    Note that a saddle point of a matrix is an element which is both the only largest element in its column and the only smallest element in its row.

    Input

    There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

    The first line contains four integers n and m (1 ≤ nm ≤ 1000) -- the number of rows and the number of columns.

    Each of the next n lines contains m integer Ai, 1, Ai, 2, ..., Aim (1 ≤ Aij ≤ 106), where Aij is the integer in the i-th row and the j-th column.

    It is guaranteed that neither the sum of all n nor the sum of all m exceeds 5000.

    Output

    For each test case, output an integer denoting the answer.

    Sample Input

    2
    2 2
    1 1
    1 1
    4 5
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    

    Sample Output

    4
    465

    思路:saddle点的定义是行最小,列最大,那么我们就统计每一个点对saddle点的贡献,即这些点是saddle点的时候,去掉当前行比他大的列与当前列比他小的行对该点的贡献无影响,即是组合数从0到x,就是2^x,列同理,就是2^(x+y),快速幂+二分查找即可
    typedef long long LL;
    
    const int MOD = 1e9+7;
    const int maxm = 1005;
    
    int A[maxm][maxm], R[maxm][maxm], C[maxm][maxm];
    
    LL quick_pow(LL a, LL b) {
        LL ret = 1;
        while(b) {
            if(b & 1) ret = (ret * a) % MOD;
            a = (a * a) % MOD;
            b >>= 1;
        }
        return ret;
    }
    
    int main() {
        int T, n, m;
        scanf("%d", &T);
        while(T--) {
            scanf("%d%d", &n, &m);
            for(int i = 1; i <= n; ++i)
                for(int j = 1; j <= m; ++j) {
                    scanf("%d", &A[i][j]);
                    R[i][j] = C[j][i] = A[i][j];
                }
            for(int i = 1; i <= n; ++i)
                sort(R[i]+1, R[i]+m+1);
            for(int i = 1; i <= m; ++i)
                sort(C[i]+1, C[i]+1+n);
                    
            LL ans = 0;
            int row, col;
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= m; ++j) {
                    row = m-(upper_bound(R[i]+1, R[i]+1+m, A[i][j]) - R[i] - 1); // larger than A[i][j] in row
                    col = lower_bound(C[j]+1, C[j]+1+n, A[i][j]) - C[j] - 1; // lower than A[i][j] in column
                    ans = (ans+quick_pow(2, col+row))%MOD;
                }
            }
            printf("%lld
    ", ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Vue3手册译稿
    捉虫日记 | MySQL 5.7.20 try_acquire_lock_impl 异常导致mysql crash
    window资源管理器无反应,右键转圈圈
    二、DHCP原理简介
    一、以太网协议
    FreeRTOS系统环境下,全局变量频繁同时在不同任务改写也可能导致硬件中断(HardFault)
    查找生成文件的进程
    解决docker容器无法访问宿主机网络的问题
    TiDB在更新版本的时候初始化Prometheus的配置文件失败
    好喜欢lodop的介绍
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12219315.html
Copyright © 2020-2023  润新知