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 Ai, j.
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 ≤ n, m ≤ 1000) -- the number of rows and the number of columns.
Each of the next n lines contains m integer Ai, 1, Ai, 2, ..., Ai, m (1 ≤ Ai, j ≤ 106), where Ai, j 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.
<h4< dd="">Output
For each test case, output an integer denoting the answer.
<h4< dd="">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
<h4< dd="">Sample Output
4 465
这题就是难在读题
我和我队友看了半天这题 觉得这题不能写
后来看题解 知道题意后 这题真的傻逼
题意
一个n*m的矩阵,问n*m个点能否在去掉某些行、列的情况下,成为马鞍点。
马鞍点是行中最小 列中最大的元素(严格大于和小于),问所有 点能够成为马鞍点的方式总数。
然后按每个点算一次贡献就好了
行中最小 就把每一行中比这个值大的数目 每一次有两种状态 选和不选
列中最大 就把每一行中比这个值小的数目 每一次有两种状态 选和不选
每个点的贡献就是 expmod(2, cnt1) * expmod(2, cnt2)
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 #include <algorithm> 6 #include <set> 7 #include <iostream> 8 #include <map> 9 #include <stack> 10 #include <string> 11 #include <vector> 12 #define pi acos(-1.0) 13 #define eps 1e-6 14 #define fi first 15 #define se second 16 #define lson l,m,rt<<1 17 #define rson m+1,r,rt<<1|1 18 #define bug printf("****** ") 19 #define mem(a,b) memset(a,b,sizeof(a)) 20 #define fuck(x) cout<<"["<<x<<"]"<<endl 21 #define f(a) a*a 22 #define sf(n) scanf("%d", &n) 23 #define sff(a,b) scanf("%d %d", &a, &b) 24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) 25 #define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d) 26 #define pf printf 27 #define FRE(i,a,b) for(i = a; i <= b; i++) 28 #define FREE(i,a,b) for(i = a; i >= b; i--) 29 #define FRL(i,a,b) for(i = a; i < b; i++) 30 #define FRLL(i,a,b) for(i = a; i > b; i--) 31 #define FIN freopen("DATA.txt","r",stdin) 32 #define gcd(a,b) __gcd(a,b) 33 #define lowbit(x) x&-x 34 #pragma comment (linker,"/STACK:102400000,102400000") 35 using namespace std; 36 typedef long long LL; 37 typedef unsigned long long ULL; 38 const int INF = 0x7fffffff; 39 const int mod = 1e9 + 7; 40 const int maxn = 1e3 + 10; 41 int mp[maxn][maxn], L[maxn][maxn], H[maxn][maxn]; 42 int t, n, m; 43 LL expmod(LL a, LL b) { 44 LL ret = 1; 45 while(b) { 46 if (b & 1) ret = ret * a % mod; 47 a = a * a % mod; 48 b = b >> 1; 49 } 50 return ret; 51 } 52 int main() { 53 sf(t); 54 while(t--) { 55 sff(n, m); 56 for (int i = 0 ; i < n ; i++) { 57 for (int j = 0 ; j < m ; j++) { 58 sf(mp[i][j]); 59 L[i][j] = mp[i][j]; 60 H[j][i] = mp[i][j]; 61 } 62 } 63 for (int i = 0 ; i < n ; i++) sort(L[i], L[i] + m); 64 for (int i = 0 ; i < m ; i++) sort(H[i], H[i] + n); 65 LL ans = 0; 66 for (int i = 0 ; i < n ; i++) { 67 for (int j = 0 ; j < m ; j++) { 68 int cnt1 = m - (upper_bound(L[i], L[i] + m, mp[i][j]) - L[i]); 69 int cnt2 = lower_bound(H[j], H[j] + n, mp[i][j]) - H[j]; 70 ans = (ans + expmod(2, cnt1) * expmod(2, cnt2) % mod) % mod; 71 } 72 } 73 printf("%lld ", ans); 74 } 75 return 0; 76 }