Game (博弈+DP)

13 篇文章 0 订阅

链接:https://www.nowcoder.com/acm/contest/123/G
来源:牛客网
 

题目描述

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 by109+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 integersai,
representing the number of stones in each pile.

输出描述:

For each test case, output one integer (modulo109+7.) in a
single line, representing the number of different ways of removing piles that
Bob can ensure his victory.
dp[i][j][k]在前i堆中,删除j堆,前i堆中剩余的堆数值异或和为k。

 

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int mod=1000000007;

int dp[1010][12][1025];
int a[N];
ll ans;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,d,i,j,k;
        scanf("%d%d",&n,&d);
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        dp[1][0][a[1]]=1;
        dp[1][1][0]=1;
        for(i=2;i<=n;i++)
        {
            for(j=0;j<=min(d,n);j++)
            {
                for(k=0;k<=1024;k++)
                {
                    dp[i][j][k]=(dp[i-1][j-1][k]+dp[i-1][j][k^a[i]])%mod;
                }
            }
        }
        ll ans=0;
        for(i=0;i<=min(n,d);i++)
            ans=(ans+dp[n][i][0])%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值