Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1367    Accepted Submission(s): 581


Problem Description
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.
 

Input
The first line contains an integer T, denoting the number of the test cases.
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.
 

Output
For each test case, output the result in one line.
 

Sample Input
2 3 1 2 3 4 1 2 3 3
 

Sample Output
1 4
 
题意:给出n个数,构造两个序列,S,T,使得第一个序列里面全部元素的异或值等于第二个序列里面全部元素的AND(&)值,而且第一个序列里全部元素的下标都小于第二个序列里全部元素的下标。求一共同拥有多少种构造方法,结果对1000000007取余。

 

 

 
思路:
 dp[i][j] 表示前 i 个元素异或等于 j 的种类数 , Dp[i][j] 表示后 i 个元素相与等于 j 的种类数
想到这儿 非常easy想到   ans = dp[i][j] * Dp[n-i][j] (0<i<n , 0<=j<=1024)  
但是这样是不正确的,由于这样会出现反复的。

 

 

就以例子二来说:   
    可算出  dp[2][3] = 1     即  S={1,2}          此时    dp[2][3]=3       即 T={3} 、{3}、{3,3}     此种情况有  1*3=3种
                dp[3][3] = 2     即  S={1,2}、{3}   此时    dp[1][3]=1        即 T={3}                       此种情况有   2*1=2种
    所以 ans = 2+3=5   可是事实上正确答案是 4  ,从上述分析可明显看出  S={1,2} ,T={3}  这样的情况反复计算了。即这样的方法是不正确的。
 
所以还需一个数组   Dp2[i][j] 表示后 i 个元素相与等于 j 的种类数 且 一定包括a[i] 。
即终于  ans = dp[i][j] * Dp2[n-i][j] (0<i<n , 0<=j<=1024)     
 
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define ll long long
using namespace std;
const ll mod=1000000007;
const int maxn=1050;
const int N=1024;
ll dp[maxn][maxn],Dp[maxn][maxn],Dp2[maxn][maxn];
int a[maxn],pre[maxn],n,m;

void input()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)  scanf("%d",&a[i]);
    memset(dp,0,sizeof(dp));
    memset(Dp,0,sizeof(Dp));
    memset(Dp2,0,sizeof(Dp2));
}

void solve()
{
    for(int i=1; i<=n; i++)
    {
        dp[i][a[i]]=1;
        for(int j=N; j>=0; j--)
        {
            dp[i][j]=(dp[i][j]+dp[i-1][j])%mod;
            dp[i][j^a[i]]=(dp[i][j^a[i]]+dp[i-1][j])%mod;
        }
    }
    //cout<<dp[2][3]<<endl;
    reverse(a+1,a+n+1);
    for(int i=1; i<=n; i++)
    {
        Dp[i][a[i]]=1;
        Dp2[i][a[i]]=1;
        for(int j=N; j>=0; j--)
        {
            Dp[i][j]=(Dp[i][j]+Dp[i-1][j])%mod;
            Dp[i][j&a[i]]=(Dp[i][j&a[i]]+Dp[i-1][j])%mod;

            Dp2[i][j&a[i]]=(Dp2[i][j&a[i]]+Dp[i-1][j])%mod;
        }
    }
    ll ans=0;
    for(int i=1; i<n; i++)
    {
        int k=n-i;
        for(int j=0; j<=N; j++)
        {
            if(dp[i][j] && Dp[k][j])
            {
                ans=(ans+dp[i][j]*Dp2[k][j]%mod)%mod;
            }
        }
    }
    printf("%I64d\n",ans%mod);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        input();
        solve();
    }
    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。