hdoj The Romantic Hero 4901 (DP 位运算) 好题

来源:互联网 发布:尚趣玩网络武神赵子龙 编辑:程序博客网 时间:2024/05/15 14:03

The Romantic Hero

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


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
231 2 341 2 3 3

Sample Output
1 4
//看了两个小时还是没看懂,唉,技术太菜。。。
//大神的思路:
题意:

有一列n个数(n<=1000,每个数的范围是[0,1024]),现在要从这列数中取出两个集合S与T,其中S中每个元素要在T的左边,现在对S的所有数做异或操作,对T中所有数做与操作,若算出了相同的数,那么当前的S,T就是所需要的,问有多少组S,T符合要求,答案对10^9+7取摸。

题解:

关键是T中的与操作,所知两个数做与操作得到的数不会比这两个数大,所以可以限制了数据的范围,首先用dp[x][y]代表从x开始往后这些数中取一部分做与操作,能得到y的个数.这个过程的复杂度是O(n*1024).再从头开始做异或操作,注意的是两个数异或得到的数是可能比这两个数大的,若计算中算出超过1024可以直接舍弃.那么取值的时候为了防止重复统计,每次都是保证了前半部分会取第i位后能得到某个异或值x的个数乘上从i+1位(不一定取i+1位)往后能通过与操作得到值x的个数.

#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#define M 1000000007#define ll long longusing namespace std;int a[1050];int s[1050];int p[1050];ll dp[1050][1050];int main(){int t,n,i,j;ll ans;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&a[i]);memset(dp,0,sizeof(dp));for(i=n;i>=1;i--){memcpy(dp[i],dp[i+1],sizeof(dp[i]));for(j=0;j<=1024;j++)dp[i][j&a[i]]=(dp[i][j&a[i]]+dp[i+1][j])%M;dp[i][a[i]]=(dp[i][a[i]]+1)%M;printf("dp[%d][%d]=..%d..\n",i,a[i],dp[i][a[i]]);}memset(p,0,sizeof(p));ans=0;for(i=1;i<=n;i++){memset(s,0,sizeof(s));for(j=0;j<=1024;j++)if((j^a[i])<=1024)s[j^a[i]]=(s[j^a[i]]+p[j])%M;s[a[i]]=(s[a[i]]+1)%M;printf("s[%d]=..%d\n",a[i],s[a[i]]);for(j=0;j<=1024;j++)ans=(ans+s[j]*dp[i+1][j])%M;for(j=0;j<=1024;j++)p[j]=(p[j]+s[j])%M;}printf("%lld\n",ans);}return 0;}

0 0
原创粉丝点击