ACM 简单DP hdu 4901 The Romantic Hero

本文探讨了一项算法竞赛中的复杂匹配问题,涉及到选择两个集合以满足特定的位操作条件。通过分析输入数据和输出结果,我们深入理解了如何在有限时间内高效地解决此类问题。文中还提供了详细的解决方案步骤,帮助参赛者更好地应对类似挑战。

 ACM 简单DP hdu 4901 The Romantic Hero


The Romantic Hero

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


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

hint:
sample2
{1 2} {3}
{1 2} {3}
{1 2} {3 3}
{3} {3}
//与数学中的集合概念不同,数字相同但位置不同算是不同的


Accepted10051546MS24056KG++

/*
 * Author: NICK WONG
 * Created Time:  7/31/2014 12:54:55
 * File Name: 
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
#define out(x) cout<<#x<<": "<<x<<endl
const double eps(1e-8);
const int maxn=1100;
const long long maxint=-1u>>1;
const long long maxlong=maxint*maxint;
typedef long long lint;
int n,a[maxn];
int f[maxn][2][maxn],g[maxn][2][maxn],ans,t[maxn][maxn];
const int mod=1000000007;

void init()
{
    cin>>n;
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
}

void work()
{
    memset(f,0,sizeof(f)); 
    memset(g,0,sizeof(g));
    memset(t,0,sizeof(t));
    for (int i=1; i<=n; i++)
    {
        for (int j=0; j<=1024; j++)
        {
            f[i][0][j]=(f[i-1][0][j]+f[i-1][1][j])%mod;
            f[i][1][j^a[i]]=((f[i][1][j^a[i]]+f[i-1][1][j])%mod+f[i-1][0][j])%mod;
        }
        f[i][1][a[i]]=(f[i][1][a[i]]+1)%mod;
    }
    for (int i=n; i>=1; i--)
    {
        for (int j=0; j<=1024; j++)
        {
            g[i][0][j]=(g[i+1][0][j]+g[i+1][1][j])%mod;
            g[i][1][j&a[i]]=((g[i][1][j&a[i]]+g[i+1][0][j])%mod+g[i+1][1][j])%mod;
        }
        g[i][1][a[i]]=(g[i][1][a[i]]+1)%mod;
    }
    ans=0;
    for (int i=n; i>=1; i--)
        for (int j=0; j<=1024; j++)
        {
            t[i][j]=(t[i+1][j]+g[i][1][j])%mod;
            ans=(ans+(((long long)f[i][1][j])*t[i+1][j])%mod)%mod;
        }
    cout<<ans<<endl;
}


int main()
{
    
    int test;
    cin >> test;
    while (test--)
    {
        init();
        work();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值