B. AND Sequences

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

  • s1=s2&s3&s4&s5=0s1=s2&s3&s4&s5=0,
  • s1&s2=s3&s4&s5=0s1&s2=s3&s4&s5=0,
  • s1&s2&s3=s4&s5=0s1&s2&s3=s4&s5=0,
  • s1&s2&s3&s4=s5=0s1&s2&s3&s4=s5=0.
  • ============================================================================================================================================
  • 既然a1=a2&a3&a4&a5..&an

那么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<iostream>
#include<string>
#include<vector>
#include<string.h>
#include<set>
#include<algorithm>
#include<queue>
#include<map>
#include<stdio.h>
#include<math.h>
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<<cnt[ans]*(cnt[ans]-1)%mod*fac[n-2]%mod<<endl;
         else
            cout<<0<<endl;

    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Master the most common algorithms and data structures, and learn how to implement them efficiently using the most up-to-date features of Swift 3 About This Book Develop a deep understanding of the collections in the Swift Standard Library with this step-by-step guide Develop native Swift data structures and algorithms for use in mobile, desktop, and server-based applications Learn about performance efficiency between different data structures and algorithms Who This Book Is For This book is for developers who want to learn how to implement and use common data structures and algorithms natively in Swift. Whether you are a self-taught developer without a formal technical background or you have a degree in Computer Science, this book will provide with the knowledge you need to develop advanced data structures and algorithms in Swift using the latest language features. What You Will Learn Get to know about the basic data structures and how to use the Swift REPL Use the Swift Standard Library collections bridging to Objective-C collections, and find out about protocol-oriented programming Find out about Swift generators and sequences, and see how to use them to implement advanced data structures such as Stack, StackList, Queue, and LinkedList Implement sorting algorithms such as Insertion Sort, Merge Sort, and Quick Sort and understand the performance trade-offs between them See how to implement various binary trees, B-Tree, and Splay Trees Perform advanced searching methods using Red-Black trees, AVL trees, and Trie trees, and take a look at several substring search algorithms Get to know about the data structures used in graphs and how to implement graphs such as depth-first search, breadth-first search, directed graphs, spanning tree, and shortest path Explore algorithm efficiency and see how to measure it In Detail Apple’s Swift language has expressive features that are familiar to those working with modern Table of Contents Chapter 1. Walking Across the Playground Chapter 2. Working with Commonly Used Data Structures Chapter 3. Standing on the Shoulders of Giants Chapter 4. Sorting Algorithms Chapter 5. Seeing the Forest through the Tree Chapter 6. Advanced Searching Methods Chapter 7. Graph Algorithms Chapter 8. Performance and Algorithm Efficiency Chapter 9. Choosing the Perfect Algorithm

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值