HDU 5119 Happy Matt Friends (14北京区域赛 类背包dp)
Happy Matt Friends
Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others)
Total Submission(s): 5188 Accepted Submission(s): 1985
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.
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.
Case #2: 2
In the first 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.
题意:题目大意:n个数,从中挑k个,使得这k个数的异或和不小于m,问有多少种挑选方法(0<=k<=n)
思路:dp[i][j]表示前 i 个数中选择一些使得异或和为j的方法数,转移方程:dp[i][j] = dp[i - 1][j] + dp[i - 1][j ^ a[i]],即等于前i-1个异或和为j的方法数(第i个数不需要进行异或)加上前i-1个异或和为j ^ a[i]的方法数(第i个数需要异或),因为j ^ a[i] ^ a[i] == j ^ (a[i] ^ a[i]) == j ^ 0 = j,最后再累计一下j大于等于m时的方法数,这题内存给的够大,也可以直接采用滚动数组
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define ll long long
int const maxn = (<<);
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} int a[];
ll dp[][maxn];
int main()
{
int t;
scanf("%d",&t);
int ca=;
while(t--)
{
int n,m; memset(dp,,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
dp[][]=;
for(int i=;i<=n;i++)
for(int j=;j<maxn;j++)
dp[i][j]=dp[i-][j]+dp[i-][j^a[i]]; //如果前i-1个数异或值是j就不需要将第i个数进行异或,如果前i-1个数有异或后是j^a[i]的,那么第i个数进行异或,刚好可以得j
ll ans=;
printf("Case #%d: ",ca++);
for(int i=m;i<maxn;i++)
ans+=dp[n][i];
printf("%lld\n",ans);
}
return ;
}
最新文章
- Eclipse设置黑色主题
- NOIP模拟赛20161007
- MSSQLSERVER服务无法启动的解决方案
- 为Page添加INotifyPropertyChanged功能
- ecshop /flow.php SQL Injection Vul
- SQL笔记-第六章,索引与约束
- (ios7) 解决Ios7中,Navigatebar 显示在主View中,和ios6 不一致问题
- 18.Android之SharedPreferences数据存储学习
- js的语句
- Android Paint的使用以及方法介绍(附源码下载)
- HTML页面的导出,包括Excel和Word导出
- C++11实现模板手柄:委托构造函数、defaultkeyword分析
- netcore2.0 ORM框架中如何配置自定义的主外键加载
- [转载]在instagram上面如何利用电脑来上传图片
- windows 安全模型简介
- Java类加载器的工作原理
- MonolithFirst
- python之re正则简单够用
- 使用redis限制ip访问次数
- 唯一ID算法之:snowflake(Java版本)