题目链接: https://codeforces.ml/contest/1513/problem/B
-
题目大意: 给定一个数组,求满足
-
a1&a2&…&ai = ai+1&ai+2&…&an 这样结构的所有排列数目
-
思路: 找出和数组内所有数字 与操作 都不影响的数字k 放在等式的左右两边 ,这样这样构造的序列一定是符合要求的。
因为左边 k & ai & a… 的值一定与 k & aj … a… 一定相等。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
typedef long long ll;
const ll mod = 1e9+7;
int n;
ll a[N];
ll fac(int a,int b) { // 排列组合 A(a,b)
ll res = 1;
for(int i=1; i<=b; i++) {
res = (res * a)%mod;
a--;
}
return res%mod;
}
int main() {
int T;
cin>>T;
while(T--) {
cin>>n;
for(int i=1; i<=n; i++) cin>>a[i];
ll k = a[1];
for(int i=2; i<=n; i++) k &= a[i]; // 关键
int cnt = 0;
for(int i=1; i<=n; i++)
if(k == a[i]) cnt ++;
if(cnt<2) puts("0"); // 若这样的数的个数小于2 则不能构造符合要求的序列
else {
cout<<(ll)(cnt%mod*(cnt-1))%mod*fac(n-2,n-2)%mod<<endl;
}
}
return 0;
}