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

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+;
const int maxm = ; int A[maxm][maxm], R[maxm][maxm], C[maxm][maxm]; LL quick_pow(LL a, LL b) {
LL ret = ;
while(b) {
if(b & ) ret = (ret * a) % MOD;
a = (a * a) % MOD;
b >>= ;
}
return ret;
} int main() {
int T, n, m;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) {
scanf("%d", &A[i][j]);
R[i][j] = C[j][i] = A[i][j];
}
for(int i = ; i <= n; ++i)
sort(R[i]+, R[i]+m+);
for(int i = ; i <= m; ++i)
sort(C[i]+, C[i]++n); LL ans = ;
int row, col;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
row = m-(upper_bound(R[i]+, R[i]++m, A[i][j]) - R[i] - ); // larger than A[i][j] in row
col = lower_bound(C[j]+, C[j]++n, A[i][j]) - C[j] - ; // lower than A[i][j] in column
ans = (ans+quick_pow(, col+row))%MOD;
}
}
printf("%lld\n", ans);
}
return ;
}

Day7 - C - Saddle Point ZOJ - 3955的更多相关文章

  1. Saddle Point ZOJ - 3955 题意题

    Chiaki has an n × m matrix A. Rows are numbered from 1 to n from top to bottom and columns are numbe ...

  2. Saddle Point ZOJ - 3955(求每个值得贡献)

    题意: 给出一个矩阵,删掉一些行和列之后 求剩下矩阵的鞍点的总个数 解析: 对于每个点 我们可以求出来 它所在的行和列  有多少比它大的 设为a 有多少比它小的 设为b 然后对于那些行和列 都有两种操 ...

  3. ZOJ 3955:Saddle Point(思维)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3955 题意:给出一个n*m的矩阵,定义矩阵中的特殊点Aij当且仅当Aij是 ...

  4. ZOJ 3955 Saddle Point

    排序. 枚举每一个格子,计算这个格子在多少矩阵中是鞍点,只要计算这一行有多少数字比他大,这一列有多少数字比他小,方案数乘一下就是这个格子对答案做出的贡献. #include<bits/stdc+ ...

  5. ZOJ 3955 Saddle Point 校赛 一道计数题

    ZOJ3955 题意是这样的 给定一个n*m的整数矩阵 n和m均小于1000 对这个矩阵删去任意行和列后剩余一个矩阵为M{x1,x2,,,,xm;y1,y2,,,,,yn}表示删除任意的M行N列 对于 ...

  6. ZOJ Saddle Point 数学思维题

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5564   根据它的定义是行最小,列最大. 可以证明鞍点是唯一的. ...

  7. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  8. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  9. day7

    本节作业: 选课系统 角色:学校.学员.课程.讲师要求:1. 创建北京.上海 2 所学校2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开3. ...

随机推荐

  1. Oracle Parallel使用方法

    一. 并行查询 并行查询允许将一个sql select语句划分为多个较小的查询,每个部分的查询并发地运行,然后将各个部分的结果组合起来,提供最终的结果,多用于全表扫描,索引全扫描等,大表的扫描和连接. ...

  2. linux文件的类型和后缀名

    还是以下图的install.log文件为例,第一栏的第一个字符为“-”代表install.log为正规档案 在linux系统中一般的档案类型为: 正规档案: 如install.log,一般的讲正规档案 ...

  3. 五 Action访问方法,method配置,通配符(常用),动态

    1 通过method配置(有点low) 建立前端JSP:demo4.jsp <%@ page language="java" contentType="text/h ...

  4. css height VS min-height

    height:容器高度固定(值是百分比时除外): min-height:容器高度小于该值时取该值,大于该值时按实际的值.应用:页面中页脚置底.

  5. 第1节 Scala基础语法:9、10、数组

    1. 定义数组时,没有new和有new是有区别的: scala> val arr3 = Array[Int](2)    此时,arr3中仅包含1个元素2arr3: Array[Int] = A ...

  6. git 常用命令记录 -- 快捷&备忘

    1.安装 略2.git拉取远程分支 git config user.name git config user.email git config --global user.name xxxx git ...

  7. 对iOS开发的一些认识

    从事iOS工作这么久了,我觉得对它的认识也越来越深刻.尤其是越发明白自己从事的工作属于“客户端开发”.“软件工程”分类中. 我更喜欢“客户端开发”这个词语,相对“前端开发”而言.因为前者更能充分说明面 ...

  8. 《精通iOS开发》书籍目录

    1.欢迎来到iOS和Swift世界 2.创建一个新项目 3.实现基本交互 4.更丰富的用户界面 5.自动旋转和自动调整大小 6.多视图应用 7.分页栏与选取器 8.表视图简介 9.导航控制器和表视图 ...

  9. Dubbo+zookeeper部署到tomcat上注意事项,遇到的问题,闪退,运行报错等

    需要下载工具zookeeper-3.4.14.tar.gz,dubbo-2.5.x.zip,apache-tomcat-8.5.47-windows-x64.zip这些官网都可以先下载到 1.最新的z ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:制作一个超小按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...