(codeforces div.2 #714) B. AND Sequences

题目链接: 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值