博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Happy Matt Friends(DP)
阅读量:6327 次
发布时间:2019-06-22

本文共 2497 字,大约阅读时间需要 8 分钟。

Happy Matt Friends

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

Total Submission(s): 3700    Accepted Submission(s): 1407

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 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.
 

Source

 

 

//题意:第一行T,然后是2个整数 n,m ,然后给出n个数字,问这些数字任意组合,求异或和大于等于m的组合个数

t题解:暴力DP即可 dp[i][j] 为前 i 个数可以组成异或和等于 j 的个数

dp[i][j] = dp [i-1][j] + dp[i-1][j^num[i]]

可以滚动,可以不滚动

1 #include 
2 #include
3 #include
4 using namespace std; 5 #define LL long long 6 #define MX 1000005 7 8 int Scan() { //输入外挂 9 int res = 0, flag = 0;10 char ch;11 if((ch = getchar()) == '-') flag = 1;12 else if(ch >= '0' && ch <= '9') res = ch - '0';13 while((ch = getchar()) >= '0' && ch <= '9')14 res = res * 10 + (ch - '0');15 return flag ? -res : res;16 }17 18 void Out(int a) { //输出外挂19 if(a < 0) { putchar('-'); a = -a; }20 if(a >= 10) Out(a / 10);21 putchar(a % 10 + '0');22 }23 24 int n,m;25 int dp[2][1<<21]; // i 个数异或和为 j 个数26 int num[MX];27 28 int main()29 {30 int T;31 cin>>T;32 for(int cnt=1;cnt<=T;cnt++)33 {34 n=Scan(),m=Scan();35 for (int i=1;i<=n;i++)36 scanf("%d",&num[i]);37 memset(dp,0,sizeof(dp));38 dp[0][0]=1;39 for (int i=1;i<=n;i++)40 {41 for (int j=0;j
View Code

 

转载于:https://www.cnblogs.com/haoabcd2010/p/6776356.html

你可能感兴趣的文章
YYHS-Floor it(递推+矩阵乘法+快速幂)
查看>>
部署ceph mds node
查看>>
基于本地模板导入
查看>>
Step By Step(Lua调用C函数)
查看>>
java的动态代理机制详解
查看>>
[LeetCode] Merge Sorted Array
查看>>
团队编程项目作业3-模块开发过程
查看>>
BZOJ1187:[HNOI2007]神奇游乐园——题解
查看>>
BZOJ3930:[CQOI2015]选数——题解
查看>>
CGI FASTCGI php-fpm
查看>>
在fragment中获取Application数据
查看>>
详解CSS float属性(转)
查看>>
利用JDBC连接Oracle数据库(转)
查看>>
songs
查看>>
vscode 解决符号无法识别的问题
查看>>
js通过replace()方法配合正则去除空格
查看>>
第一课:HTML
查看>>
[cb]Unity 项目架构
查看>>
Java基本语法-----java流程控制语句
查看>>
【面试 网络协议】【第十四篇】网络协议篇
查看>>