Black is gifted with a Divine array aa consisting of nn (1≤n≤20001≤n≤2000) integers. Each position in aa has an initial value. After shouting a curse over the array, it becomes angry and starts an unstoppable transformation.
The transformation consists of infinite steps. Array aa changes at the ii-th step in the following way: for every position jj, ajaj becomes equal to the number of occurrences of ajaj in aa before starting this step.
Here is an example to help you understand the process better:
Initial array: | 22 11 11 44 33 11 22 |
After the 11-st step: | 22 33 33 11 11 33 22 |
After the 22-nd step: | 22 33 33 22 22 33 22 |
After the 33-rd step: | 44 33 33 44 44 33 44 |
... | ... |
In the initial array, we had two 22-s, three 11-s, only one 44 and only one 33, so after the first step, each element became equal to the number of its occurrences in the initial array: all twos changed to 22, all ones changed to 33, four changed to 11 and three changed to 11.
The transformation steps continue forever.
You have to process qq queries: in each query, Black is curious to know the value of axax after the kk-th step of transformation.
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001≤t≤1000). Description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤20001≤n≤2000) — the size of the array aa.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the initial values of array aa.
The third line of each test case contains a single integer qq (1≤q≤1000001≤q≤100000) — the number of queries.
Next qq lines contain the information about queries — one query per line. The ii-th line contains two integers xixi and kiki (1≤xi≤n1≤xi≤n; 0≤ki≤1090≤ki≤109), meaning that Black is asking for the value of axiaxi after the kiki-th step of transformation. ki=0ki=0 means that Black is interested in values of the initial array.
It is guaranteed that the sum of nn over all test cases doesn't exceed 20002000 and the sum of qq over all test cases doesn't exceed 100000100000.
Output
For each test case, print qq answers. The ii-th of them should be the value of axiaxi after the kiki-th step of transformation. It can be shown that the answer to each query is unique.
Input:
2
7
2 1 1 4 3 1 2
4
3 0
1 1
2 2
6 1
2
1 1
2
1 0
2 1000000000
Out:
1
2
3
3
1
2
关键思路:
两个数与运算结果 <= 较小者
一群数与运算结果 <= 最小者
代码:
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i = a; i <= b; ++ i)
#define per(i, a, b) for(int i = a; i >= b; -- i)
using namespace std;
const int N = 2e5 + 10,mod = 1e9+ 7;
int a[N];
int find(int t){
int ans = 1;
rep(i,1,t){
ans *= i;
ans %= mod;
}
return ans;
}
void solve(){
int n; cin >> n;
rep(i,1,n) cin >> a[i];
sort(a+1,a+n+1);
int sum = 1;
for(;a[1] == a[sum] && sum <= n;sum++);
sum--;
if(sum < 2) cout << 0 << endl;
else{
rep(i,1,n){
if((a[i]&a[1]) != a[1]){
cout << 0 << endl;
return;
}
}
sum%= mod;
int ans = sum*(sum-1)%mod;
ans *= find(n-2);
cout << (ans%mod )<< endl;
}
}
signed main()
{
int t; cin >> t;
while(t--) solve();
return 0;
}