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

时间:2023-03-08 17:12:44
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],这个数组所代表的意思是,前 i 个数中选取某些数(必须包括 i )组成的集合按位异或得到j^a[i]

但是发现这样会重复,因为对于对于S和T的选取的元素,下标前后没有明显的界限,从前往后找与从后往前找,两厢重复。后来就重新设了一个数组去存以前的所有的值:

_xor[i][j^a[i]] += xort[i-1][j] ;
xort[i][j] = _xor[i][j] + xort[i-1][j] ;//这个表达式,代表这前 i 个中选取了某些数(必须选了 i )异或后的值为j的情况加上前 i -1个选某些值异或后的值为 j 。其实就是选与不选 i 的两种情况相加,组成的就是前 i 个中选取某些数异或出 j 所组成的情况,此时的 i 可选可不选。

 //
#include <cstring>
#include <cstdio>
#include <iostream>
typedef long long LL ;
const long long mod = ;
using namespace std ; int a[] ;
LL _xor[][],xort[][],_and[][],andt[][] ; void Init()
{
memset(_xor,,sizeof(_xor)) ;
memset(xort,,sizeof(xort)) ;
memset(_and,,sizeof(_and)) ;
memset(andt,,sizeof(andt)) ;
memset(a,,sizeof(a)) ;
}
int main()
{
int T,n ;
cin >> T ;
while(T -- )
{
cin >> n ;
Init() ;
for(int i = ; i < n ; i++)
cin >> a[i] ;
_xor[][a[]] = xort[][a[]] = ;
for(int i = ; i < n ; i++)
{
for(int j = ; j < ; j ++)
{
_xor[i][j^a[i]] += xort[i-][j] ;
_xor[i][j^a[i]] %= mod ;
}
_xor[i][a[i]] ++ ;
for(int j = ; j < ; j++)
{
xort[i][j] = _xor[i][j] + xort[i-][j] ;
xort[i][j] %= mod ;
}
}
_and[n-][a[n-]] = andt[n-][a[n-]] = ;
for(int i = n- ; i >= ; i--)
{
for(int j = ; j < ; j++)
{
_and[i][j&a[i]] += andt[i+][j] ;
_and[i][j&a[i]] %= mod ;
}
_and[i][a[i]] ++ ;
for(int j = ; j < ; j++){
andt[i][j] = andt[i+][j] + _and[i][j] ;
andt[i][j] %= mod ;
}
}
LL ans = ;
for(int i = ; i < n- ; i++)
{
for(int j = ; j < ; j++)
{
ans += (_xor[i][j] * andt[i+][j]) % mod ;
ans %= mod ;
}
}
cout << ans << endl ;
}
return ;
}