Matrices with XOR property

本文探讨了一种特殊矩阵的构造问题,该矩阵需满足元素唯一且遵循特定的异或值大小关系。通过给出具体的实现代码,文章展示了如何计算符合这些条件的矩阵数量,并通过实例解释了算法的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Imagine A is a NxM matrix with two basic properties

1) Each element in the matrix is distinct and lies in the range of 1<=A[i][j]<=(N*M)

2) For any two cells of the matrix, (i1,j1) and (i2,j2), if (i1^j1) > (i2^j2) then A[i1][j1] > A[i2][j2] ,where

1 ≤ i1,i2 ≤ N

1 ≤ j1,j2 ≤ M.

^ is Bitwise XOR

Given N and M , you have to calculatethe total number of matrices of size N x M which have both the properties

mentioned above.

Input format:

First line contains T, the number of test cases. 2*T lines follow with N on the first line and M on the second, representing the number of rows and columns respectively.

Output format:

Output the total number of such matrices of size N x M. Since, this answer can be large, output it modulo 10^9+7

Constraints:

1 ≤ N,M,T ≤ 1000

SAMPLE INPUT

1

2

2

SAMPLE OUTPUT

4

Explanation

The four possible matrices are:

[1 3] | [2 3] | [1 4] | [2 4]

[4 2] | [4 1] | [3 2] | [3 1]

题意 构造一个矩阵,使得在矩阵中ij位置,值为a[i],异或值为i^j,求得比它小的异或值的位置值比它小。且保证矩阵中的数唯一,1~n*m;
暴力做法,三维矩阵异或值图 0 3 2 0 填 1 2 3的排列 为 3! 1填 4 5 2填 6 7
3 0 1 3填 7 8
2 1 0
排列数为 3!*2!*2!*2!
由于1000^1000 最大为 1023 .所以暴力可求。 算出所有的 A!

#include <bits/stdc++.h>
using namespace std;
const int maxn=2333;
const int mod=1e9+7;
const int mm=1e6+7;
int n,m;
int cnt[maxn];
int fact[mm];
int main()
{
    fact[0]=1;
    for(long long i=1;i<=1e6;i++)
        fact[i]=i*fact[i-1]%mod;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(cnt,0,sizeof(cnt));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                cnt[i^j]++;
        }
        long long ans=1;
        for(int i=0;i<1024;i++)
        {
            if(cnt[i])
            {
                ans=ans*fact[cnt[i]]%mod;
            }
        }
    printf("%lld\n",ans );
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值