题目约束
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.
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.
算法思路
- 其实这是思路十分简单的一道题
- 我们从一列数当中选择两个集合,第二个集合一定在第一个集合的后面。而且第二个集合中的所有的数的相与值和第一个集合所有的数异或的值相等。
- 比较简单的一个思路是dp1[i][j]表示的是以Arr[i]最后一个元素(包括最后一个元素),相异或的值为j。相似的,dp2[i][j]是以Arr[i]最前面的元素,相与的值为j的组合的数量。这样的话,我们可以得到一个O(n3)的算法。
- 但是,这样的话应该会超时,我们加一个新的数组,dp3[i][j]指的是,以Arr[i]为界,相与的值或者相异或的值为j的组合的数目.这样的话,我们无论是计算还是累加的值都会得到一个O(n2)的值。
- 唯一的问题是精度问题。
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
#define MAXN 1005
#define MX 1026
#define MOD 1000000007
#define LL long long
int t;
LL dp1[MAXN][MX];
LL dp2[MAXN][MX];
LL dp3[MAXN][MX];
int n;
int Arr[MAXN];
void Init()
{
int i,j;
int tmp;
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
memset(dp3,0,sizeof(dp3));
dp1[1][Arr[1]] = 1;
for(i=2;i<=n;i++){
dp1[i][Arr[i]]++;
for(j=0;j<=MX-2;j++){
dp1[i][j] = (dp1[i][j]+dp1[i-1][j])%MOD;
tmp = j^Arr[i];
dp1[i][tmp] = (dp1[i][tmp]+dp1[i-1][j])%MOD;
}
}
dp2[n][Arr[n]]=1;
dp3[n][Arr[n]]=1;
for(i=n-1;i>=1;i--){
dp2[i][Arr[i]]++;
dp3[i][Arr[i]]++;
for(j=0;j<=MX-2;j++){
dp2[i][j] = (dp2[i][j]+dp2[i+1][j])%MOD;
tmp = j&Arr[i];
dp2[i][tmp] = (dp2[i+1][j]+dp2[i][tmp])%MOD;
dp3[i][tmp] = (dp2[i+1][j]+dp3[i][tmp])%MOD;
}
}
return;
}
int main()
{
freopen("input","r",stdin);
int i,j;
long long ans;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
ans = 0;
for(i=1;i<=n;i++)
scanf("%d",&Arr[i]);
Init();
for(i=1;i<n;i++){
for(j=0;j<=MX-2;j++){
ans = (ans+(LL)(dp1[i][j]*dp3[i+1][j])%MOD)%MOD;
}
}
printf("%lld\n",ans);
}
return 0;
}