山东省第九届ACM: G:Game(DP.动态规划)

题目描述

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 时先手必输,

题意: B 拿取0-D 个 使得 B 取胜 的方案数,

思路:  明显的 DP,赛时竟然没看出来,一直在二进制上考虑,但是仔细一想,往深一层就是DP啊.

唉.

dp[i][j][k]  代表 第 i 堆石子  取走 j 堆  剩下的异或为 k 的方案数

那么 必有 dp[i][j][k] = dp[i-1][j][k] + dp[i-1][j-1][k^x]

就是这样, 


code

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MOD =  1e9+7;
const int mod =  1e9+7;

ll dp[1100][12][1100];
int main()
{
    int t;
    int n,d;
    cin>>t;
    while(t--)
    {
        cin>>n>>d;
        memset(dp,0,sizeof(dp));
        for(int i = 0 ;i <= n; i++)
            dp[i][0][0] = 1;

        ll x,sum = 0;
        for(int i = 1;i <= n ; i++)
        {
            cin>>x;
            sum^=x;
            for(int j = d;j >0 ;j--)
            {
                for(int k = 0 ; k <1024 ; k++)
                    dp[i][j][k] =  (dp[i-1][j][k]+dp[i-1][j-1][k^x])%mod;
            }
        }
        ll ans = 0;
        for(int i = 0 ;i <= d; i++)
        {
            ans = (ans+ dp[n][i][sum])%mod;
        }
        cout<< (ans%mod)<<endl;
    }
    return 0;
}

posted @ 2018-05-16 20:15  Sizaif  阅读(344)  评论(0编辑  收藏  举报