ZOJ 3955 想法

Saddle Point

Time Limit: 1 Second       Memory Limit: 131072 KB

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({i1i2, ..., is}, {j1j2, ..., jt}) be the matrix that results from deleting row i1i2, ..., is and column j1j2, ..., jt of A and f({i1i2, ..., is}, {j1j2, ..., jt}) be the number of saddle points in matrix M({i1i2, ..., is}, {j1j2, ..., jt}).

Chiaki would like to find all the value of f({i1i2, ..., is}, {j1j2, ..., 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, 1Ai, 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


题意:给出一个n*m的矩阵,定义矩阵中的特殊位置(i, j)为当且仅当Aij 是这一行唯一最小的数,是这一列唯一最大的数
删去一些行和列,剩下的元素构成的矩阵一共有(2^n-1)*(2^m-1)种,求这些矩阵的特殊位置的数量之和


题解:我们可以算每个数对答案的贡献

设第 i 行比a[i][j]大的数有 k1 个

设第 j 列比a[i][j]小的数有 k2 个

则a[i][j]对答案的贡献就是 2^k1*2^k2


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[1005][1005],row[1005][1005],col[1005][1005];
const ll mod=1000000007;
ll quick(ll a,ll k){
    ll ans=1;
    while(k){
        if(k&1)ans=ans*a%mod;
        k/=2;
        a=a*a%mod;
    }
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m,i,j;
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                scanf("%lld",&a[i][j]);
                row[i][j]=a[i][j];
                col[j][i]=a[i][j];
            }
        }
        for(i=1;i<=n;i++)sort(row[i]+1,row[i]+1+m);
        for(i=1;i<=m;i++)sort(col[i]+1,col[i]+1+n);
        ll ans=0;
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                ll t1=upper_bound(row[i]+1,row[i]+1+m,a[i][j])-row[i];
                ll t2=lower_bound(col[j]+1,col[j]+1+n,a[i][j])-col[j];
                t1=m-t1+1;
                t2--;
                ans=(ans+quick(2,t1)*quick(2,t2)%mod)%mod;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值