[UPC] 山东省第九届省赛 Games | dp

简介: 题意:有n堆石子,后手可以在游戏开始之前挪走不超过d堆,然后问后手赢得游戏的方案数量 % 1e9 + 7思路:先求出所有数的亦或和设d p [ i ] [ j ] [ k ] dp[i][j][k]dp[i][j][k]为前 i ii 堆石子挪走 j jj 堆,并且亦或和为 k kk 的方案数然后进行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 109+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 ≤ 103, d ≤ 10, ai ≤ 103


输出


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


题意:


有n堆石子,后手可以在游戏开始之前挪走不超过d堆,然后问后手赢得游戏的方案数量 % 1e9 + 7


思路:


先求出所有数的亦或和

设d p [ i ] [ j ] [ k ] dp[i][j][k]dp[i][j][k]为前 i ii 堆石子挪走 j jj 堆,并且亦或和为 k kk 的方案数

然后进行dp


转移方程还是比较好想的:

d p [ i ] [ j ] [ k ] = d p [ i − 1 ] [ j ] [ k ] + d p [ i − 1 ] [ j − 1 ] [ k   x o r   a [ i ] ] dp[i][j][k] = dp[i-1][j][k] + dp[i-1][j-1][k\ xor \ a[i]]dp[i][j][k]=dp[i−1][j][k]+dp[i−1][j−1][k xor a[i]]


#define Clear(x,val) memset(x,val,sizeof x)
int n,a[maxn],d;
ll dp[1003][11][1025];/// dp[i][j][k] -> pre i select j ^ == k  => ways
void slove() {
    for(int i=0; i<=n; i++) dp[i][0][0] = 1;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=d; j++) {
            for(int k=0; k<=1024; k++) {/// 1000 ^
                dp[i][j][k] = (dp[i-1][j][k] + dp[i-1][j-1][k ^ a[i]]) % mod;
            }
        }
    }
}
int main() {
    int _ = read;
    while(_ --) {
        n = read,d = read;
        ll sum = 0;
        for(int i=1; i<=n; i++) a[i] = read,sum ^= a[i];
        slove();
        ll ans = 0;
        for(int i=0; i<=d; i++) {
            ans += dp[n][i][sum];
            ans %= mod;
        }
        cout << ans << endl;
    }
    return 0;
}
/**
**/
/**************************************************************
    Problem: 6992
    Language: C++
    Result: 正确
    Time:257 ms
    Memory:90772 kb
****************************************************************/


文章知识点与官方知识档案匹配,可进一步学习相关知识

算法技能树leetcode-动态规划22-括号生成8061 人正在系统学习中

目录
相关文章
|
SQL 数据安全/隐私保护 Python
湖北省工匠杯预赛WriteUP
湖北省工匠杯预赛WriteUP
122 0
|
安全 前端开发 JavaScript
[SWPUCTF 2021 新生赛]gift_F12
[SWPUCTF 2021 新生赛]gift_F12
194 0
|
Python
2021 山东省赛 H . Adventurer’s Guild(01背包)
2021 山东省赛 H . Adventurer’s Guild(01背包)
74 0
|
机器学习/深度学习
山东第一届省赛 Balloons (dfs)
山东第一届省赛 Balloons (dfs)
56 0
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
91 0
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
|
存储 缓存 算法
蓝桥每日一点题,国赛场上ta和你
蓝桥每日一点题,国赛场上ta和你
70 0
蓝桥每日一点题,国赛场上ta和你
|
C++ 容器
蓝桥每日一点题,国赛场上TA和你(2)
蓝桥每日一点题,国赛场上TA和你(2)
85 0
蓝桥每日一点题,国赛场上TA和你(2)
2020ICPC昆明M.Stone Games(主席树)
2020ICPC昆明M.Stone Games(主席树)
83 0
UPC组队赛第三场——G: The Famous ICPC Team Again (主席树)
UPC组队赛第三场——G: The Famous ICPC Team Again (主席树)
100 0
[SDUT 2414] | 山东省第三届省赛 An interesting game | 最小费用最大流
题目描述 Xiao Ming recently designs a little game, in front of player there are N small hillsides put in order, now Xiao Ming wants to increase some hillsides to block the player, so he prepared another M hillsides, but he does not hope it will be too difficult,
164 0
[SDUT 2414] | 山东省第三届省赛 An interesting game | 最小费用最大流