HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119

Problem Description

Matt has N friends. They are playing a game together.

Each of Matt’s friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends’magic numbers is no less than M , Matt wins.

Matt wants to know the number of ways to win.

Input

The first line contains only one integer \(T\) , which indicates the number of test cases.

For each test case, the first line contains two integers \(N, M (1 \le N \le 40, 0 \le M \le 10^6)\).

In the second line, there are \(N\) integers \(k_i (0 ≤ k_i ≤ 10^6)\), indicating the \(i\)-th friend’s magic number.

Output

For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y indicates the number of ways where Matt can win.

Sample Input

2
3 2
1 2 3
3 3
1 2 3

Sample Output

Case #1: 4
Case #2: 2

Hint

In the ?rst sample, Matt can win by selecting:

friend with number 1 and friend with number 2. The xor sum is 3.

friend with number 1 and friend with number 3. The xor sum is 2.

friend with number 2. The xor sum is 2.

friend with number 3. The xor sum is 3. Hence, the answer is 4.

Source

2014ACM/ICPC亚洲区北京站-重现赛(感谢北师和上交)

Solution

题意

给定 \(n\) 个数 \(k[i]\),从中取出一些数使得异或和大于等于 \(m\),求有几种取法。

思路

背包DP 滚动数组

设 \(dp[i][j]\) 表示前 \(i\) 个数中异或和为 \(j\) 的所有取法。状态转移方程为 \(dp[i][j] = dp[i - 1][j] + dp[i - 1][j\ xor\ k[i]]\)。

由于当前状态只和前一个状态有关,因此可以用滚动数组优化。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1 << 20;

ll dp[10][maxn];
ll k[50];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    for(int _ = 1; _ <= T; ++_) {
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        int n, m;
        cin >> n >> m;
        for(int i = 1; i <= n; ++i) {
            cin >> k[i];
        }
        for(int i = 1; i <= n; ++i) {
            for(int j = 0; j < maxn; ++j) {
                dp[i & 1][j] = dp[(i - 1) & 1][j] + dp[(i - 1) & 1][j ^ k[i]];
            }
        }
        ll ans = 0;
        for(int i = m; i < maxn; ++i) {
            ans += dp[n & 1][i];
        }
        cout << "Case #" << _ << ": " << ans << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wulitaotao/p/11517978.html

时间: 2024-10-29 19:09:55

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)的相关文章

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

HDU 5119 Happy Matt Friends(DP)

求解方案数的简单DP,比赛时没有往DP上想,思维比较局限. 状态转移很好写,类似于背包,我用记忆化搜索写的容易写,但是效率比较低,还占内存,读者可以改成递推式,还可以改成滚动数组,因为每一层的状态只用到它上一层的状态 . 细节参见代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<map

HDU - 1024 Max Sum Plus Plus(dp+滚动数组优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. 题解:dp[i][j]从前j个数字中选择i段,然后根据第j个数字是否独立成一段,可以写出 状态转移方程:dp[i][j]=max(dp[i][j-1]+num[j],max(dp[i-1][k])+num[j]) 这里的max(dp[i-

HDU 1024 Max Sum Plus Plus (DP&#183;滚动数组)

题意  从n个数的数组中选出不相交的m段  求被选数的和的最大值 Max Sum 的升级版  不只是要选一段连续的了  而是选m段  思想还是类似  依旧dp 状态和状态转移方程不是很难想  在 Max Sum 这个问题中 dp[i] 表示的是以第i个数结尾的一段的 Max Sum  由于这里还有一个多少段的状态  于是这里令 dp[i][j] 表示在前 i 个数中选取 j 组  且第 i 个数在最后一组中的 Max Sum Plus Plus 那么现在对于第i个数有两种决策 1,  第 i 个

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

HDU 5616 Jam&#39;s balance 背包DP

Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell whether things on different side are the same weight.Weights can be put on left side or right side arbitrarily.Please tell whether the balance can me

HDU 5617 Jam&#39;s maze dp+滚动数组

题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=666&pid=1003 题解: 设dp[x1][x2][i]表示第i步时,从(1,1)点走到了(x1,y1),(n,n)点走到了(x2,y2)点的合法的总数. 1 #include<iostream> 2 #include

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)

Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Ale