“浪潮杯”山东省第九届ACM大学生程序设计竞赛 G Games(DP)

Problem Description

Alice and Bob are playing a stone game. There are nnn 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 ddd. Note ddd can be greater than nnn, and in that case you can remove all of the piles.
Let ansansans 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 ansansans divided by 109+710^9+7109+7.
Input

The first line contains an integer TTT, representing the number of test cases.
For each test cases, the first line are two integers nnn and ddd, which are described above.
The second line are nnn positive integers aia_iai​, representing the number of stones in each pile.
T≤5,n≤103,d≤10,ai≤103T \leq 5, n \leq 10^3, d \leq 10, a_i \leq 10^3T≤5,n≤103,d≤10,ai​≤103
Output

For each test case, output one integer (modulo 109+710^9 + 7109+7) in a single line, representing the number of different ways of removing piles that Bob can ensure his victory.
Sample Input

2
5 2
1 1 2 3 4
6 3
1 2 4 7 1 2

Sample Output

2
5

Hint
Source
“浪潮杯”山东省第九届ACM大学生程序设计竞赛(感谢山东财经大学)

看上去是博弈(其实看上去并不像),比赛时候写了一发2^1000次幂的暴力果不其然t了,后来回来知道是dp后瞎写了一发状态转移方程。竟然写对了,泪奔。。。。不过还是有地方没有想明白,虽然知道了方程但是还是隔了好久才A。
题意:在正常nim博弈的基础上,后手可以选择不超过d堆石子,在游戏之前全部拿走,使后手必胜,问有多少种拿法。
题解:
要求不超过d堆石子使剩余异或和为0,即拿不超过d堆石子,这些拿走的石子的异或和为所有石子异或和。
D P [ i ] [ j ] [ k ] : i j k
那么很容易写出状态转移方程
d p [ i ] [ j ] [ k ] = d p [ i 1 ] [ j ] [ k ] + d p [ i 1 ] [ j 1 ] [ n u m [ i ] k ] / / n u m [ i ] i
这里要知道如果当前异或和为k,那么假如是拿了num[i]之后到达了k,那么拿之前的异或值即为num[i]异或k。
不过这里还有小细节,数据范围小于 10 3 ,之前一直以为1000和小于1000的异或会接近2000,其实最多不会超过1024。所以一直mle还以为要滚动数组。。。。。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int dp[1001][12][1040];
const int mod = 1e9+7;
int main()
{
    int t,n,m;
    int num[1005];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        memset(num,0,sizeof num);
        memset(dp,0,sizeof dp);
        int maxm = -1,sum = 0;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d",&num[i]);
            sum ^= num[i];
        }
        for (int i = 1; i <= n; i++)
        {
            dp[i][0][0] = 1;
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m && j <= i; j++)
            {
                for (int k = 0; k <= 1024; k++)
                {
                    if(i == 1)dp[i][j][num[i]] = 1;
                    else dp[i][j][k] = (dp[i - 1][j][k] + dp[i -1][j - 1][k ^ num[i]])%mod;
                }
            }
        }
        int ans = 0;
        for (int i = 0; i <= m; i++)
        {
            ans = (ans + dp[n][i][sum]) % mod;
        }
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/meituanwaimai/article/details/80377756