Total Submission(s): 1415 Accepted Submission(s): 599
You may wonder why this country has such an interesting tradition? It has a very long story, but I won't tell you :).
Let us continue, the party princess's knight win the algorithm contest. When the devil hears about that, she decided to take some action.
But before that, there is another party arose recently, the 'MengMengDa' party, everyone in this party feel everything is 'MengMengDa' and acts like a 'MengMengDa' guy.
While they are very pleased about that, it brings many people in this kingdom troubles. So they decided to stop them.
Our hero z*p come again, actually he is very good at Algorithm contest, so he invites the leader of the 'MengMengda' party xiaod*o to compete in an algorithm contest.
As z*p is both handsome and talkative, he has many girl friends to deal with, on the contest day, he find he has 3 dating to complete and have no time to compete, so he let you to solve the problems for him.
And the easiest problem in this contest is like that:
There is n number a_1,a_2,...,a_n on the line. You can choose two set S(a_s1,a_s2,..,a_sk) and T(a_t1,a_t2,...,a_tm). Each element in S should be at the left of every element in T.(si < tj for all i,j). S and T shouldn't be empty.
And what we want is the bitwise XOR of each element in S is equal to the bitwise AND of each element in T.
How many ways are there to choose such two sets? You should output the result modulo 10^9+7.
For each test case, the first line contains a integers n.
The next line contains n integers a_1,a_2,...,a_n which are separated by a single space.
n<=10^3, 0 <= a_i <1024, T<=20.
2 3 1 2 3 4 1 2 3 3
1 4
#include<iostream>
using namespace std;
#define mod 1000000007
typedef __int64 ll;
const int MAXN = 1005;
const int MAXS = 1025;
/**
*其中and1主要是用来辅助计算必须包含a[i]的情况的
*/
int xor[MAXN][MAXS],and1[MAXN][MAXS],and2[MAXN][MAXS];
int a[MAXN];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
memset(xor,0,sizeof(xor));
memset(and1,0,sizeof(and1));
memset(and2,0,sizeof(and2));
//xor
xor[1][a[1]] = 1;
for(i=2;i<n;i++){
xor[i][a[i]]++ ;
for(int j=0;j<MAXS;j++){
if(xor[i-1][j]){
//加入a[i]
int t = j^a[i];
xor[i][t] += xor[i-1][j];
xor[i][t] %= mod;
//不加a[i]
//这里用+=而不是=
xor[i][j] += xor[i-1][j];
xor[i][j] %= mod;//开始没加,wa
}
}
}
and1[n][a[n]] = 1;
and2[n][a[n]] = 1;
//and
for(i=n-1;i>0;i--){
and1[i][a[i]]++;
and2[i][a[i]]++;
for(int j=0;j<MAXS;j++){
if(and1[i+1][j]){
int t = j & a[i];
//加入a[i]
and1[i][t] += and1[i+1][j];
and1[i][t] %= mod;
and2[i][t] += and1[i+1][j];
and2[i][t] %= mod;
//不加a[i]
and1[i][j] += and1[i+1][j];
and1[i][j] %= mod;
}
}
}
int res = 0;
for(i=1;i<n;i++){
for(int j=0;j<MAXS;j++){
if(xor[i][j]&&and2[i+1][j]){
res += ((ll)xor[i][j])*and2[i+1][j]%mod;
res %= mod;
}
}
}
printf("%d\n", res);
}
return 0;
}
892




被折叠的 条评论
为什么被折叠?



