Happy Matt Friends(dp)

本文介绍了一个关于选择朋友魔法数值进行异或操作的游戏问题。玩家需要通过选择特定的朋友来达到或超过目标数值以获胜。文章提供了两种解决方案:一种是使用动态规划结合位运算的方法,另一种是采用递归深度优先搜索的暴力求解方式。

Happy Matt Friends

Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Others)
Total Submission(s): 1810    Accepted Submission(s): 715


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 ≤ 10 6).

In the second line, there are N integers ki (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 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范围为40,权值范围为10^6 约等于2的20次方,可以考虑类似dp+位运算的做法。因为异或值一定不会大于2^20,
所以可以枚举这个值,所以可以列出方程f[i][j]=f[i][j]+f[i][j^a[i]];
另外我用暴力也写了一发,虽然知道10000%超时;
dp代码:
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 const int INF=0x3f3f3f3f;
 7 const int MAXN=1<<20;
 8 int dp[2][MAXN<<1];//刚开始开到MAXN+10 RE,改成这才对; 
 9 int main(){
10     int T,N,M,flot=0;
11     int m[41];
12     scanf("%d",&T);
13     while(T--){
14         scanf("%d%d",&N,&M);
15         memset(dp,0,sizeof(dp));
16         dp[0][0]=1;
17         int x=0;
18         for(int i=0;i<N;i++){
19             x^=1;
20             scanf("%d",m+i);
21             for(int j=0;j<=MAXN;j++){
22                 dp[x][j]=dp[x^1][j]+dp[x^1][j^m[i]];
23                 //代表上一次值为j^m[i]现在再^m[i]等于j了再加上上次j的个数; 
24             }
25         }
26         long long ans=0;
27         for(int i=M;i<=MAXN;i++)ans+=dp[x][i];
28         printf("Case #%d: %lld\n",++flot,ans);
29     }
30     return 0;
31 }

暴力超时:

 1 #include<stdio.h>
 2 int ans,dt[41];
 3 int N,M;
 4 void dfs(int top,int sox,int num,int t){
 5     if(num==t){
 6         if(sox>=M)ans++;
 7         return;
 8     }
 9     for(int i=top;i<N;i++){
10         dfs(i+1,sox^dt[i],num+1,t);
11     }
12 }
13 int main(){
14     int T;
15     scanf("%d",&T);
16     while(T--){
17         scanf("%d%d",&N,&M);
18         for(int i=0;i<N;i++)scanf("%d",dt+i);
19         ans=0;
20         for(int i=1;i<=N;i++){
21             dfs(0,0,0,i);
22         }
23         printf("%d\n",ans);
24     }
25     return 0;
26 }

 

转载于:https://www.cnblogs.com/handsomecui/p/4905304.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值