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;
- }
-
-
相关阅读:
OpenGL | 通过绘制一个三角形来入门 OpenGL 图形渲染管线
Flink 侧输出流(SideOutput)
基于HTML+CSS+JS制作商城(web前端网页制作课作业)---手机主题 7页
串的匹配 (Brute - Force 算法)
算法学习:LeetCode-593. 有效的正方形
SpringMVC基础概述
DPDK 数据传输流程
【进程管理】进程状态
深入学习 Redis Cluster - 基于 Docker、DockerCompose 搭建 Redis 集群,处理故障、扩容方案
Redis的主从复制
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126178657