HDU4901 The Romantic Hero 计数DP

本文介绍了一道算法竞赛题目,要求从给定的数列中选出两个集合,其中一个集合的所有元素异或结果等于另一个集合的所有元素按位与的结果,并统计这种组合的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Romantic Hero

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


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
 


题意:

    给一列数,让你选两个集合,A集合所有元素下标小于B集合所有元素下标,A集合的所有元素异或等于B集合所有元素AND,两个集合都非空,求集合元素有多少种选法,MOD 10^9+7。0<=元素大小<1024,最多1000个元素。

 

题解:

    f[i][j]:由0~i的元素异或得到j的种类数。

    h[i][j]:由i~n-1的元素AND得到j的种类数。

    g[i][j]:由i~n-1的元素,且一定包含i,AND得到j的种类数。

求出这些,最后把f[i][j]*g[i+1][j]求和就得到答案了!

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<math.h>

#define ll __int64
#define usint unsigned int
#define mz(array) memset(array, 0, sizeof(array))
#define RE  freopen("1.in","r",stdin)
#define WE  freopen("mask.txt","w",stdout)

#define maxa 1024
#define maxn 1000
#define C 1000000007

int f[maxn-1][maxa],g[maxn][maxa],h[maxn][maxa];

int main() {

    int a[maxn],T,n;
    short i,j,t;
    int ans;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        for(i=0; i<n; i++)
            scanf("%d",&a[i]);
        mz(f);
        mz(g);
        mz(h);
        f[0][a[0]]=1;
        for(i=1; i<n-1; i++) {
            f[i][a[i]]++;///单独一个元素的集合的情况
            for(j=0; j<maxa; j++) {
                if(f[i-1][j]) {
                    f[i][j]+=f[i-1][j];///继承之前算好的情况(就是 不包括当前元素的情况)
                    f[i][j]%=C;
                    t=j^a[i];
                    f[i][t]+=f[i-1][j];///由前一次的情况异或当前元素得到的情况(包括当前元素的情况)
                    f[i][t]%=C;
                }

            }
        }

        g[n-1][a[n-1]]=1;
        h[n-1][a[n-1]]=1;
        for(i=n-2; i>0; i--) {
            g[i][a[i]]++;
            h[i][a[i]]++;
            for(j=0; j<maxa; j++) {
                if(h[i+1][j]) {
                    h[i][j]+=h[i+1][j];
                    h[i][j]%=C;
                    t=j&a[i];
                    h[i][t]+=h[i+1][j];
                    h[i][t]%=C;

                    g[i][t]+=h[i+1][j];///包括当前元素的情况(g没有不包括当前元素的情况)
                    g[i][t]%=C;
                }

            }
        }
        ans=0;
        for(i=0; i<n-1; i++)
            for(j=0; j<maxa; j++) {
                if(f[i][j]&&g[i+1][j]) {
                    ans+=(((ll)f[i][j])*(g[i+1][j])%C);
                    ans%=C;
                }
            }
        printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值