码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4901 The Romantic Hero(DP)

时间:2014-08-01 00:00:10      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   for   ar   代码   

HDU 4901 The Romantic Hero

题目链接

题意:给定一个序列,要求找一个分界点,然后左边选一些数异或和,和右边选一些数且和相等,问有几种方法

思路:dp,从左往右和从右往左dp,求出异或和且的个数,然后找一个分界点,使得一边必须在分界点上,一边随意,然后根据乘法原理和加法原理计算

代码:

#include <cstdio>
#include <cstring>

typedef __int64 ll;
const int N = 1024;
const int M = 2048;

const int MOD = 1000000007;

int t, n, a[N], l[N][M], r[N][N], ls[N][M], rs[N][N];

int main() {
    scanf("%d", &t);
    while (t--) {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	    scanf("%d", &a[i]);
	memset(l, 0, sizeof(l));
	memset(ls, 0, sizeof(ls));
	l[1][a[1]] = ls[1][a[1]] = 1;
	for (int i = 2; i <= n; i++) {
	    for (int j = 0; j < M; j++)
		l[i][a[i]^j] = (l[i][a[i]^j] + ls[i - 1][j]) % MOD;
	    l[i][a[i]] = (l[i][a[i]] + 1) % MOD;
	    for (int j = 0; j < M; j++)
		ls[i][j] = (ls[i - 1][j] + l[i][j]) % MOD;
	}
	memset(r, 0, sizeof(r));
	memset(rs, 0, sizeof(rs));
	r[n][a[n]] = rs[n][a[n]] = 1;
	for (int i = n - 1; i >= 1; i--) {
	    for (int j = 0; j < N; j++)
		r[i][a[i]&j] = (r[i][a[i]&j] + rs[i + 1][j]) % MOD;
	    r[i][a[i]] = (r[i][a[i]] + 1) % MOD;
	    for (int j = 0; j < N; j++)
		rs[i][j] = (rs[i + 1][j] + r[i][j]) % MOD;
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++)
	    for (int j = 0; j < N; j++) {
		ans += ((ll)ls[i][j] * r[i + 1][j] % MOD);
		ans %= MOD;
	    }
	printf("%I64d\n", ans);
    }
    return 0;
}


HDU 4901 The Romantic Hero(DP),布布扣,bubuko.com

HDU 4901 The Romantic Hero(DP)

标签:style   http   color   os   io   for   ar   代码   

原文地址:http://blog.csdn.net/accelerator_/article/details/38325675

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!