Divide by Zero 2021 and Codeforces Round #714 (Div. 2)B. AND Sequences
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A sequence of nn non-negative integers (n≥2n≥2) a1,a2,…,ana1,a2,…,an is called good if for all ii from 11 to n−1n−1 the following condition holds true:
a1&a2&…&ai=ai+1&ai+2&…&an,a1&a2&…&ai=ai+1&ai+2&…&an,
where && denotes the bitwise AND operation.
You are given an array aa of size nn (n≥2n≥2). Find the number of permutations pp of numbers ranging from 11 to nn, for which the sequence ap1ap1, ap2ap2, ... ,apnapn is good. Since this number can be large, output it modulo 109+7109+7.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104), denoting the number of test cases.
The first line of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the size of the array.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) — the elements of the array.
It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.
Output
Output tt lines, where the ii-th line contains the number of good permutations in the ii-th test case modulo 109+7109+7.
Example
input
Copy
4 3 1 1 1 5 1 2 3 4 5 5 0 2 0 3 0 4 1 3 5 1
output
Copy
6 0 36 4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 66 permutations possible with numbers from 11 to 33: [1,2,3][1,2,3], [1,3,2][1,3,2], [2,1,3][2,1,3], [2,3,1][2,3,1], [3,1,2][3,1,2], [3,2,1][3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 3636 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3][1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]s=[0,0,3,2,0]. This is a good sequence because
那么a1=a1&a2&a3....&an
也有an=a1&a2&a3...&an
所以a1和an的二进制pos位置如果是1,那么a1...anpos位置全是1,否则,必然有一个0
这样
a1&a2 ? a3......&an
单独看pos位置,是1当今当全是1,是0的话,左右两边的a1,an就完美的控制了永远是0,无论新加的数字是多少,这一位置都是0
方案数计算组合数即可,排列
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- typedef long long int ll;
- map<int,ll>cnt;
-
- # define mod 1000000007
-
- ll fac[200000+10];
-
- void init()
- {
- fac[0]=1;
-
- for(int i=1; i<=200000; i++)
- {
- fac[i]=fac[i-1]*i%mod;
- }
- }
-
- ll a[2000000+10];
-
- int main()
- {
-
-
- init();
-
- int t;
-
- cin>>t;
-
- while(t--)
- {
- int n;
-
- cnt.clear();
- cin>>n;
- cin>>a[1];
- ll ans=a[1];
- cnt[a[1]]++;
- for(int i=2; i<=n; i++)
- {
- cin>>a[i];
- ans=(ans&a[i]);
- cnt[a[i]]++;
- }
-
- if(cnt[ans]>=2)
- cout<
-1)%mod*fac[n-2]%mod< - else
- cout<<0<
-
- }
- return 0;
- }
-
-
相关阅读:
2022年web前端开发学习路线图
前端项目使用指定字体样式
一百七十六、Kettle——Kettle配置HDFS输出控件能不能加GZIP等压缩方式?
GUI 应用:socket 网络聊天室
edm开发信
三维模型3DTile格式轻量化压缩的遇到常见问题与处理方法分析
Spring (5)—声明式事务控制
05预测识别-依托YOLO V8进行训练模型的识别——对视频中的图片进行识别
Mac Goland无法调试
JS简单实现随机颜色验证码功能
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126178657