时间限制: 2 Sec 内存限制: 128 MB
提交: 70 解决: 17
[提交][状态][讨论版][命题人:admin]
题目描述
Alice and Bob are playing a stone game. There are n piles of stones. In each turn, a player can remove some stones from a pile (the number must be positive and not greater than the number of remaining stones in the pile). One player wins if he or she remove the last stone and all piles are empty. Alice plays first.
To make this game even more interesting, they add a new rule: Bob can choose some piles and remove entire of them before the game starts. The number of removed piles is a nonnegative integer, and not greater than a given number d. Note d can be greater than n, and in that case you can remove all of the piles.
Let ans denote the different ways of removing piles such that Bob are able to win the game if both of the players play optimally. Bob wants you to calculate the remainder of ans divided by 10^9+7..
输入
The first line contains an integer T, representing the number of test cases.
For each test cases, the first line are two integers n and d, which are described above.
The second line are n positive integers ai, representing the number of stones in each pile.
T ≤ 5, n ≤ 10^3, d ≤ 10, ai ≤ 10^3
输出
For each test case, output one integer (modulo 10^9 + 7) in a single line, representing the number of different ways of removing piles that Bob can ensure his victory.
样例输入
2
5 2
1 1 2 3 4
6 3
1 2 4 7 1 2
样例输出
2
5
这道题大概意思就是说要你取出一组数,使剩下的数的抑或和为0,找到有几种组合可以满足条件。由于最多不能取出超过10个,当然是要找取出的组合而非剩余的组合了,数据较大,不能暴力枚举。
虽然看起来挺像博弈论的,其实是三维DP,找组合的思路是前i个元素,取出j个,抑或和为k的取法数量有dp[i][k][j]个,先求出原组合的抑或和为x,扫一遍,求出∑dp[n][x][j],就是答案了,由于抑或运算不会超过最大值的二进制的最高位,所以每次扫到最高位即可,注意数组不要开小,虽然最大值是1000,但抑或运算可能达到1024-1,所以数组要开到1024以上。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int A[1002][12][1030];
int B[1005];
int C[12];
const int M=1000000007;
int n,d,t,ans,m;
int finl;
int i,j,k,h;
int main()
{
C[0]=1;
for(i=1; i<=10; i++)
C[i]=C[i-1]*2;
scanf("%d",&t);
while(t--)
{
ans=0;
finl=0;
m=0;
memset(A,0,sizeof(A));
scanf("%d%d",&n,&d);
d=min(d,n);
for(i=1; i<=n; i++)
{
scanf("%d",&B[i]);
m=max(m,(int)B[i]);
ans^=B[i];
A[i][1][B[i]]=(A[i][1][B[i]]+1)%M;
}
for(i=0; i<=10; i++)
if(C[i]>m)
{
m=C[i];
break;
}
//cout<<m<<endl;
for(i=1; i<=n; i++)
{
for(j=0; j<=m; j++)
{
for(k=1; k<=d; k++)
{
A[i][k][j]%=M;
A[i-1][k][j]%=M;
A[i-1][k-1][B[i]^j]%=M;
A[i][k][j]=(A[i][k][j]+(A[i-1][k][j]+A[i-1][k-1][B[i]^j])%M)%M;
}
}
}
/*for(i=1;i<=d;i++)
{
for(j=0;j<m;j++)
{
for(k=1;k<=n;k++)
cout<<A[k][i][j]<<' ';
cout<<endl;
}
cout<<endl;
}*/
if(ans==0) finl++;
for(i=1; i<=d; i++)
{
finl=(finl+A[n][i][ans])%M;
}
printf("%d\n",finl);
}
return 0;
}
来源:oschina
链接:https://my.oschina.net/u/4395975/blog/3968089