HDOJ 5119 Happy Matt Friends DP

N*M暴力DP....

Happy Matt Friends

Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Others)

Total Submission(s): 82    Accepted Submission(s): 34

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 ≤ N ≤ 40, 0 ≤ M ≤ 106).

In the second line, there are N integers ki (0 ≤ ki ≤ 106), 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亚洲区北京站-重现赛(感谢北师和上交)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int LL;

LL dp[2][1<<20+1];
int a[100];
int n,m;

int main()
{
    int T_T,cas=1;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%d%d",&n,&m);
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            scanf("%d",a+i);
        int now=1,pre=0;
        dp[0][0]=1LL;
        for(int i=1;i<=n;i++)
        {
            memset(dp[now],0,sizeof(dp[now]));
            for(int j=0;j<=(1<<20);j++)
            {
                if(dp[pre][j])
                {
                    int x=j^a[i];
                    dp[now][x]+=dp[pre][j];
                    dp[now][j]+=dp[pre][j];
                }
            }
            swap(now,pre);
        }
        LL ans=0;
        for(int i=m;i<=(1<<20);i++)
            ans+=dp[pre][i];
        printf("Case #%d: %I64d\n",cas++,ans);
    }
    return 0;
}
时间: 2025-01-21 03:25:13

HDOJ 5119 Happy Matt Friends DP的相关文章

HDU 5119 Happy Matt Friends(DP)

题目链接:传送门 题意: 给定n个数,求从中选出任意个数异或起来值大于m的方案数. 分析: 动态规划,设dp[i][j] 表示第几次选第i个数的时候异或起来 值为j的方案数.dp[i][j^a[i]]+=dp[i][j];但是对空间有要求 我们可以用滚动数组来写. 代码如下: #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn = 1&l

HDU 5119 Happy Matt Friends(dp+位运算)

题意:给定n个数,从中分别取出0个,1个,2个...n个,并把他们异或起来,求大于m个总的取法. 思路:dp,背包思想,考虑第i个数,取或者不取,dp[i][j]表示在第i个数时,异或值为j的所有取法.dp[i][j] = dp[i - 1][j] + dp[i - 1][j ^ a[i]]); 其中dp[i - 1][j]表示不取第i个数,dp[i - 1][j & a[i]]表示取第i个数,由于40比较大,所以用滚动数组优化,后一个状态需要前一个来推导,而和前一个之前的所有的没有关系,所以之

HDU 5119 Happy Matt Friends(DP || 高斯消元)

题目链接 题意 : 给你n个数,让你从中挑K个数(K<=n)使得这k个数异或的和小于m,问你有多少种异或方式满足这个条件. 思路 : 正解据说是高斯消元.这里用DP做的,类似于背包,枚举的是异或的和,给定的数你可以选择放或者不放,dp[i][j]代表的是前 i 个数中选择k个异或的和为j. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #define LL long long

hdoj 2391 Filthy Rich 【DP】

题目大意:有个二维数组,你从(0,0)出发,最终到(n,m), 在这个二维数组中,每个位置dp[i][j]都有一定量的黄金,你可以拾取,问你最多能失去多少,并且,你的方向有下,右, 斜向下三个方向: 策略:就是每一个都加上它的上方向与左方向的最大值,这样到最后就是最大值.详情见代码 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2391 代码: #include<stdio.h> #include<string.h> int dp[1

hdoj 4901 The Romantic Hero DP hdoj 4902 Nice boat 线段树

惨遭丽洁乱虐..这一场也是比得乱七八糟的,4902本是丽洁定义比较难的题,结果数据随机的,被许多暴力水过了..4905考察的是四边形不等式优化,但是这道题的dp方程实际上不满足该优化的条件..朴素的o(n^3)会超时,所以这题目前是没有正解了..我还写了个这题的贪心,强度挺高,可以对大概一半数据,错的误差也只有个位数,还揪出官方第五个数据..朴素dp和贪心跑这个数据都比官方数据多了1,也就证明这题不满足四边形不等式优化的条件.. http://acm.hdu.edu.cn/showproblem

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'ma

HDU 5119 Happy Matt Friends(简单二维dp)

题意不再说了,就是一个二维的dp,维持取最大值是多少. Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Others) Total Submission(s): 608    Accepted Submission(s): 229 Problem Description Matt has N friends. They are playing a ga

HDU 5119 Happy Matt Friends(DP)

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

HDU 5119 Happy Matt Friends ——(背包DP)

题意:有最多40个数字,取任意个数字他们的异或和>=k则是可行的方案,问有多少种可行的方案. 分析:dp[now][j]表示当前这个值的种类数,那么转移方程为dp[now][j] = dp[pre][j] + dp[pre][j^a[i]].因为a^b=c的话,c^b=a,所以j^a[i]就可以得到通过异或a[i]转移到j的那个状态了.然后可以用滚动数组压缩一下. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #inc