当前位置: 代码迷 >> 综合 >> 第九届山东省赛G题 Games 【三维DP】【nim博弈】【异或结论】
  详细解决方案

第九届山东省赛G题 Games 【三维DP】【nim博弈】【异或结论】

热度:7   发布时间:2023-11-11 10:52:12.0

6992: Games

时间限制: 2 Sec   内存限制: 128 MB
提交: 55   解决: 13
[提交][状态][讨论版][命题人: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


题目大意:

Bob和Alice 再玩游戏。

游戏的内容是: 有n堆石子,两个人轮流从中取石子,每次只能从一堆里拿,可以一次拿完一堆。先取完的是胜者。

但是,再游戏开始之前,Bob可以先从这n堆中取走d堆石子,d可能比n要大。问有多少种取法能让Bob获胜。Alice先手取石子。

思路:

首先根据Nim 博弈内容,入果所有石子堆的异或和结果为0,那么先手必败。

所以我们的目的就是从n堆中取走d堆,使得剩下堆异或和结果为0。


状态转移方程:dp[i][j][k] = dp[i-1][j][k^stone[i]] + dp[i-1][j-1][k]

其中dp[i][j][k] 代表前i个数去掉j个然后异或和为K的方案数目

设x^stone[i]=k ,x=k^stone[i]

dp[i-1][j][k^stone[i]] 代表 不去掉第j个石头堆的状态,那么此状态由dp[i-1][j][x] ^ k转换而来

dp[i-1][j-1][k] 代表去掉第j个石堆的状态

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 1e3+5;
const int INF = 0x3f3f3f3f;
const LL  MOD = 1e9+7;
int n,d;
LL ans;
LL stone[MAXN];
LL dp[MAXN][15][1026];///dp[i][j][k] 代表前i个数去掉j个然后异或和为K的方案
/*设x^stone[i]=k ,x=k^stone[i]dp[i-1][j][k^stone[i]] 代表 不去掉第j个的状态,那么此状态由dp[i-1][j][x] ^ k转换而来dp[i-1][j-1][k] 代表去掉第j个的状态任意数的异或和不会大于1023*/
int main()
{int T; cin>>T;while(T--){M(dp,0);ans = 0 ;scanf("%d %d",&n,&d);for(int i=1;i<=n;i++) scanf("%lld",&stone[i]);dp[1][0][stone[1]]=1;  ///代表前1个数中不去掉第一个石堆的状态dp[1][1][0] = 1;       ///代表前一个数中去掉第一个数的状态for(int i=2;i<=n;i++){for(int j = 0;j<=min(n,min(d,i));j++)///因为d可能比n大,所以要取最小值{for(int k=0;k<=1024 ;k++){///dp[i][j][k] = dp[i-1][j][k^stone[i]] + dp[i-1][j-1][k]dp[i][j][k] = (dp[i-1][j][k^stone[i]]%MOD + dp[i-1][j-1][k]%MOD)%MOD;}}}for(int i=0;i<=min(n,d);i++){ans = (ans%MOD + dp[n][i][0]%MOD)%MOD;///然后求出异或结果为0的状态和就好}printf("%lld\n",ans);}return 0;
}