The Romantic Hero

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

#include<cstdio>
#include<cstring>  

typedef __int64 LL;
#define mod 1000000007
const int MAXN = 1002;
const int MAXA = 1025;
int dp1[MAXN][MAXA], dp2[MAXN][MAXA], dp3[MAXN][MAXA];
int a[MAXN];  

int main()
{
    int T, n, i, j, t;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        for(i = 0; i < n; i++)
            scanf("%d",&a[i]);
        memset(dp1, 0, sizeof(dp1));
        memset(dp2, 0, sizeof(dp2));
        memset(dp3, 0, sizeof(dp3));
        dp1[0][a[0]] = 1;
        for(i = 1; i < n - 1; i++) {
            dp1[i][a[i]]++; //单独一个元素构成一个集合
            for(j = 0; j < MAXA; j++) {
                if(dp1[i-1][j]) {
                    dp1[i][j] += dp1[i-1][j]; //不添加第i个元素进行异或,继承之前算好的
                    dp1[i][j] %= mod;  

                    t = j ^ a[i];  //添加第i个元素进行异或
                    dp1[i][t] += dp1[i-1][j];
                    dp1[i][t] %= mod;
                }
            }
        }
        dp2[n-1][a[n-1]] = 1;
        dp3[n-1][a[n-1]] = 1;
        for(i = n-2; i > 0; i--) {
            dp2[i][a[i]]++;
            dp3[i][a[i]]++;   //单独一个元素构成一个集合
            for(j = 0; j < MAXA; j++) {
                if(dp2[i+1][j]) {
                    dp2[i][j] += dp2[i+1][j];  //不添加第i个元素进行按位与
                    dp2[i][j] %= mod;  

                    t = j & a[i]; //添加第i个元素进行按位与
                    dp2[i][t] += dp2[i+1][j];
                    dp2[i][t] %= mod;  

                    dp3[i][t] += dp2[i+1][j]; //添加第i个元素进行按位与
                    dp3[i][t] %= mod;
                }
            }
        }
        int ans = 0;
        for(i = 0; i < n - 1; i++) {
            for(j = 0; j < MAXA; j++) {
                if(dp1[i][j] && dp3[i+1][j]) {
                    ans += (LL(dp1[i][j]) * dp3[i+1][j] % mod);
                    ans %= mod;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 const int maxn=1e3+10;
 6 const int maxm=1024*2;
 7 const int mod=1e9+7;
 8 int n,a[maxn];
 9 int dp[maxn][maxm][2],dps[maxn][maxm][2];
10 int main()
11 {
12     int T;
13     scanf("%d",&T);
14     while(T--)
15     {
16         scanf("%d",&n);
17         int maxi=0;
18         for(int i=1;i<=n;i++)
19             scanf("%d",&a[i]);
20         memset(dp,0,sizeof(dp));
21         memset(dps,0,sizeof(dps));
22         for(int i=1;i<=n;i++)
23         {
24             dp[i][a[i]][0]=1;
25             dps[i][a[i]][0]=1;
26             for(int j=0;j<maxm;j++)
27                 if(dp[i-1][j][0])
28                 {
29                     dp[i][j][0]=(dp[i][j][0]+dp[i-1][j][0])%mod;
30                     dp[i][j^a[i]][0]=(dp[i][j^a[i]][0]+dp[i-1][j][0])%mod;
31                     dps[i][j^a[i]][0]=(dps[i][j^a[i]][0]+dp[i-1][j][0])%mod;
32                 }
33         }
34         for(int i=n;i>=1;i--)
35         {
36             dp[i][a[i]][1]=1;
37             for(int j=0;j<maxm;j++)
38                 if(dp[i+1][j][1])
39                 {
40                     dp[i][j][1]=(dp[i][j][1]+dp[i+1][j][1])%mod;
41                     dp[i][j&a[i]][1]=(dp[i][j&a[i]][1]+dp[i+1][j][1])%mod;
42                 }
43         }
44         long long ans=0;
45         for(int i=1;i<n;i++)
46             for(int j=0;j<maxm;j++)
47                 if(dps[i][j][0])
48                     ans=(ans+(long long)dps[i][j][0]*(long long)dp[i+1][j][1])%mod;
49         printf("%I64d\n",ans);
50     }
51     return 0;
52 } 

The Romantic Hero

时间: 2024-08-27 10:05:39

The Romantic Hero的相关文章

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a

HDU 4901 The Romantic Hero(DP)

HDU 4901 The Romantic Hero 题目链接 题意:给定一个序列,要求找一个分界点,然后左边选一些数异或和,和右边选一些数且和相等,问有几种方法 思路:dp,从左往右和从右往左dp,求出异或和且的个数,然后找一个分界点,使得一边必须在分界点上,一边随意,然后根据乘法原理和加法原理计算 代码: #include <cstdio> #include <cstring> typedef __int64 ll; const int N = 1024; const int

HDU 4901 The Romantic Hero 题解——S.B.S.

The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1675    Accepted Submission(s): 705 Problem Description There is an old country and the king fell in love with a devil. The de

HDU 4901 The Romantic Hero

The Romantic Hero Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u 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 k

HDU 4901 The Romantic Hero(二维dp)

题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候按照给的先后数序取数,后面的里面的所有的元素的下标一定比前面的大.问你有多上种放元素的方法可以使得前面异或的值和后面与的值相等. dp[x][y] 表示走到第x步,得到y这个数字一共有多少种方法. 但是需要注意这里得分一下,不能直接用dp数组存种数,你需要分一下从上一层过来的次数,和这一层自己可以到达的次数.然后取和的时候前后两个集合的种数进行乘法,注意边乘边取余. 顺便给一组数据: 4 3

HDU4901 The Romantic Hero 计数DP

2014多校4的1005 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 393    Accepted Submission(s): 150 Problem Description There i

hdu 4901 The Romantic Hero (dp+背包问题)

题意: 有n个数,从n个数中选出两个集合s和集合t,保证原序列中,集合s中的元素都在 集合t中元素的左边.且要求集合s中元素做抑或运算的值与集合t中元素做与运算的 值相等.问能选出多少种这样的集合s和t. 算法: 左右dp. 用dp[i][j]表示前i个数 做抑或运算得到j的方法数.最后一个值取不取到都不一定. 故为背包的问题.右边也是一样. 枚举时可能出现重复.枚举到第i个和枚举第i+1个可能重复.所以要枚举一个中间值. 这个中间值是归到s集的,因为抑或支持逆运算,而与是不支持的. 所以最后d

HDU 4901(杭电多校训练#3 1005题)The Romantic Hero(DP)

题目地址:HDU 4901 这题没想到最后居然能够做出来.... 这题用了两次DP,先从前往后求一次异或的,再从后往前求一次与运算的.分别是 1:求异或的时候,定义二维数组huo[1000][1024],前者指第几位,后者是哈希的思想,若huo[x][y]=2则表示最右边的数为第x位时,异或值为y的出现了两次,需要再定义一个hash数组,来保存前面出现的所有情况,再找有多少位的时候,用hash数组中出现的所有的值与当前的第x位的数字进行异或. 2:求与的时候,定义二维数组yu[1000][102

2014多校第四场1005 || HDU 4901 The Romantic Hero (DP)

题目链接 题意 :给你一个数列,让你从中挑选一些数组成集合S,挑另外一些数组成集合T,要求是S中的每一个数在原序列中的下标要小于T中每一个数在原序列中下标.S中所有数按位异或后的值要与T中所有的数按位与的值相同,问能找出多少符合要求的组合. 思路 :比赛的时候有点没有头绪,后来二师兄想出了状态转移方程,YN又改了很多细节,最后才A的.总之是个别扭的DP..... 一开始是 _xor[i][j^a[i]] += _xor[i-1][j] :j 的下一个状态 就是异或上a[i],这个数组所代表的意思

hdoj 4901 The Romantic Hero DP hdoj 4902 Nice boat 线段树

惨遭丽洁乱虐..这一场也是比得乱七八糟的,4902本是丽洁定义比较难的题,结果数据随机的,被许多暴力水过了..4905考察的是四边形不等式优化,但是这道题的dp方程实际上不满足该优化的条件..朴素的o(n^3)会超时,所以这题目前是没有正解了..我还写了个这题的贪心,强度挺高,可以对大概一半数据,错的误差也只有个位数,还揪出官方第五个数据..朴素dp和贪心跑这个数据都比官方数据多了1,也就证明这题不满足四边形不等式优化的条件.. http://acm.hdu.edu.cn/showproblem