hdu 4901 The Romantic Hero (dp)

题目要求将一个序列分成两个集合A,B,并且A中的所有元素异或的结果要等于B中所有元素与的结果,求这样的划分方案,注意的是题目要求了:这两个集合的元素必须满足对于任意的i,j ai的序号小于bj的序号。

其实就是类似背包的问题,求出正推反推对的方案数,然后枚举端点两端相乘。注意集合非空。

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define B(x) (1<<(x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned ui;
const int oo = 0x3f3f3f3f;
//const ll OO = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define lson rt<<1
#define rson rt<<1|1
void cmax(int& a, int b){ if (b > a)a = b; }
void cmin(int& a, int b){ if (b < a)a = b; }
void cmax(ll& a, ll b){ if (b > a)a = b; }
void cmin(ll& a, ll b){ if (b < a)a = b; }
void cmax(double& a, double b){ if (a - b < eps) a = b; }
void cmin(double& a, double b){ if (b - a < eps) a = b; }
void add(int& a, int b, int mod){ a = (a + b) % mod; }
void add(ll& a, ll b, ll mod){ a = (a + b) % mod; }
const ll MOD = 1000000007;
const int maxn = 1100;
ll f[maxn][maxn][2], g[maxn][maxn][2];
int a[maxn];

int main(){
	int n, T;
	scanf("%d", &T);
	while (T--){
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]);
		memset(f, 0, sizeof f);
		memset(g, 0, sizeof g);
		for (int i = 1; i <= n; i++){
			f[i][a[i]][1] = 1;
			for (int j = 0; j < 1024; j++){
				f[i][j][0] += f[i - 1][j][0];
				f[i][j][0] += f[i - 1][j][1];
				f[i][j][0] %= MOD;
				f[i][j^a[i]][1] += f[i - 1][j][0];
				f[i][j^a[i]][1] += f[i - 1][j][1];
				f[i][j^a[i]][1] %= MOD;
			}
		}
		for (int i = n; i >= 1; i--){
			g[i][a[i]][1] = 1;
			for (int j = 0; j < 1024; j++){
				g[i][j][0] += g[i + 1][j][0];
				g[i][j][0] += g[i + 1][j][1];
				g[i][j][0] %= MOD;
				g[i][j&a[i]][1] += g[i + 1][j][0];
				g[i][j&a[i]][1] += g[i + 1][j][1];
				g[i][j&a[i]][1] %= MOD;
			}
		}
		ll ans = 0;
		for (int i = 1; i < n; i++){
			for (int j = 0; j < 1024; j++){
				ans += (f[i][j][1] * (g[i + 1][j][0] + g[i + 1][j][1]) % MOD) % MOD;
				ans %= MOD;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


你可能感兴趣的:(dp,HDU)