Happy Matt Friends
Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others)Total Submission(s): 1250 Accepted Submission(s): 483
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.
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.
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: 2HintIn 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
Recommend
多次思考,你会发现这个题的难点就是如何能计算出方法数,又不重复地选择朋友。
这就是递推的好处,不仅简化了问题(不是一步求解),而且可以按照自己顺手或着觉得容易的顺序或方式来解决问题。
int a[maxn],n,m;
const int ed= (1<< 21);
ll dp[2][ ed+10 ];
int main()
{
// cout<<ed<<endl;
int T,kase=0;scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(dp,0,sizeof dp);
int now=0,pre;
dp[now][0]=1;
for(int i=1;i<=n;i++)
{
pre=now;
now=1^now;
for(int st=0;st<ed;st++)
{
//写法1: dp[now][(a[i]^st) ]=dp[pre][(a[i]^st) ]+dp[pre][st];
dp[now][st]=dp[pre][st]+dp[pre][(a[i]^st)]; //写法2:这里利用了异或性质 x^x=0;
}
/*
写法3:
以下的写法是在dp[now][st]可以从多个dp[pre][]到达时的写法
所以以上两种写法,是基于每个dp[i][st]只会从某个dp[pre][]到达,
即状态转移非常确定,就是异或一个定值a[i];
memset(dp[now],0,sizeof dp[now]);
for(int st=0;st<ed;st++)
{
dp[now][(a[i]^(st) )]+=dp[pre][st];
}
for(int st=0;st<ed;st++)
{
dp[now][st]=dp[now][st]+dp[pre][st];
}
*/
}
ll ans=0;
for(int i=m;i<ed;i++)
{
ans+=dp[now][i];
}
printf("Case #%d: %lld\n",++kase,ans);
}
return 0;
}
头文件:
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
using namespace std;
const int INF =0x3f3f3f3f;
const int maxn= 45 ;
const int maxm= 10000000 ;
//const int INF= ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123 20+1
2016-07-04
/*
2016-07-04
*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 40 ;
const int maxV=(1<<21);
ll dp[2][maxV+8];
int n,m,x,ed;
int main()
{
int T,kase=0;scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int now=1,nex=0;
memset(dp,0,sizeof dp);
dp[nex][0]=1;
int x;
for1(i,n)
{
swap(now,nex);
scanf("%d",&x);
for(int v=0;v<maxV;v++)//=maxV是不可能的
{
dp[nex][v]=dp[now][v];
}
for(int v=0;v<maxV;v++)
{
int v2=v^x;
dp[nex][v2]+=dp[now][v];
}
}
ll ans=0;
for(int v=m;v<maxV;v++)
{
ans+=dp[nex][v];
}
printf("Case #%d: %lld\n",++kase,ans);
}
return 0;
}