Alice and Bob HDU - 4111 (SG函数)

本文探讨了一个由Alice和Bob发明的游戏策略,游戏中包含两种操作:减少数字或合并数字,目标是在所有数字被消除后判断胜者。文章通过分析操作次数范围和特殊情况,提出了一种使用动态规划解决游戏结局的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they always find the best strategy, and that's why they feel bored again and again. They just invented a new game, as they usually did. 
The rule of the new game is quite simple. At the beginning of the game, they write down N random positive integers, then they take turns (Alice first) to either: 
1. Decrease a number by one. 
2. Erase any two numbers and write down their sum. 
Whenever a number is decreased to 0, it will be erased automatically. The game ends when all numbers are finally erased, and the one who cannot play in his(her) turn loses the game. 
Here's the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!

InputThe first line contains an integer T(1 <= T <= 4000), indicating the number of test cases. 
Each test case contains several lines. 
The first line contains an integer N(1 <= N <= 50). 
The next line contains N positive integers A 1 ....A N(1 <= A i <= 1000), represents the numbers they write down at the beginning of the game.OutputFor each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is either "Alice" or "Bob".

Sample Input

3
3
1 1 2
2
3 4
3
2 3 5

Sample Output

Case #1: Alice
Case #2: Bob
Case #3: Bob

给出 n 个数,每次有两种操作
1、任意取一个数字,让他减一
2、把两个数字合并起来
最后不能操作的人失败

考虑全部不合并和全部合并的情况,操作次数的范围是[∑a, ∑a + n - 1]。
如果每一堆石子全部大于1, ∑a + n - 1 就是先手能否赢。 因为如果这是一个奇数,后手想赢必须让其中一堆没有参与到合并,那么后手一定要让这一堆进行操作1,由于全部大于1,那么不可能一步完成,先手只要每次把这堆合并起来就一定能赢。
反之同理,偶数时后手用同样的方法限制先手,后手一定能赢。
那么考虑有 1 的时候,如果这时候一方想要减少合并次数,只需要把 1 进行操作1,另一方无法阻止。则把 1 单独拿出来。
用 sg[i][j] 表示有 i 个 1 ,除了 1 以外剩余堆可以进行的最多的操作次数。然后开始操作
(1)、在 i 里面进行操作1
(2)、在 j 里面进行操作1
(3)、让 i 和 j 进行操作2
(4)、让 i 和 i 进行操作2
然后进行转移就可以了
然后其中的细节差不多有如下
1、当 i == 0时,j 的奇偶进就决定了能否赢
2、在(2)中,如果 j==2,会转化一个到 i,自己变成0,变成sg[i+1][0]; 如果j>2,正常转化,sg[i+1][j-1];
3、在(4)中,如果 j==0,转化出来为sg[i-2][2]; 如果 j>0,那么合并出来的新堆可以之前的 j 在合并一次,所以是sg[i-2][j+3]
/*
          .
         ';;;;;.
        '!;;;;;;!;`
       '!;|&#@|;;;;!:
      `;;!&####@|;;;;!:
     .;;;!&@$$%|!;;;;;;!'.`:::::'.
     '!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
     :!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%`    '!%&#########@$!:.
     ;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
     ;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
    `!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
    :@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
    ;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
   `!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|.
%&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##!
!%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&:
':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%.
 !@|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&'
  .:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@:
  .%$;;;;;;;;;;;;;;;;;;|$$$$@&|;;;;;;;;;;;;;;;;;;;;%@%.
    !&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#;
     `%$!;;;;;;;;;;;$@|;;;;;;;;;;;;;;;;;;;;;;;;!%$@#@|.
       .|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%$$$$$@#|.
           ;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|.
           |##$|!;;;;;;::'':;;;;;;;;;;;;;!%$$$@#@;
          ;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|`
        .%##@|;;;;:::''''''''''::;!%&##$'
      `$##@$$@@&|!!;;;:'''''::::;;;;;|&#%.
    ;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@:
     .%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%.
    :@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;`
     `%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&'
     |##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
    :;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$:                 !@#$'
      |##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%:              '%%!%&;
      |&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||`            '%$|!%&;
      |@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&:           .|%;:!&%`
      !@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$;          `%&%!!$&:
      '$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##;         !$!;:!$&:
       |#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%'      `%$|%%;|&$'
        |&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&;  .:%&$!;;;:!$@!
        `%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
        !&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
       '%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
       .!%%&%'`|$;       :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pii        pair<int, int>
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)

typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 5e4 + 50;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

int n, m;
int cas, tol, T;

int sg[55][maxn];

void dfs(int x, int y) {
    if(sg[x][y] != -1)    return ;
    if(x == 0) {
        sg[x][y] = y&1;
        return ;
    }
    if(x) {
        dfs(x-1, y);
        if(sg[x-1][y] == 0) {
            sg[x][y] = 1;
            return ;
        }
    }
    if(y) {
        dfs(x-1, y+1);
        if(sg[x-1][y+1] == 0) {
            sg[x][y] = 1;
            return ;
        }
    }
    if(x > 1) {
        if(y) {
            dfs(x-2, y+3);
            if(sg[x-2][y+3] == 0) {
                sg[x][y] = 1;
                return ;
            }
        } else {
            dfs(x-2, 2);
            if(sg[x-2][2] == 0) {
                sg[x][y] = 1;
                return ;
            }
        }
    }
    if(y > 1) {
        if(y > 2) {
            dfs(x, y-1);
            if(sg[x][y-1] == 0) {
                sg[x][y] = 1;
                return ;
            }
        } else {
            dfs(x+1, 0);
            if(sg[x+1][0] == 0) {
                sg[x][y] = 1;
                return ;
            }
        }
    }
    sg[x][y] = 0;
    return ;
}

int main() {
    mes(sg, -1);
    sg[0][0] = 0;
    cas = 1;
    scanf("%d", &T);
    while(T--) {
        int ans = 0, cnt = 0;
        scanf("%d", &n);
        for(int i=1, x; i<=n; i++) {
            scanf("%d", &x);
            if(x > 1)
                ans += x+1;
            else
                cnt++;
        }
        if(ans)
            ans--;
        if(sg[cnt][ans] == -1)
            dfs(cnt, ans);
        printf("Case #%d: %s\n", cas++, sg[cnt][ans] ? "Alice" : "Bob");
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Jiaaaaaaaqi/p/10352275.html

资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 华为移动服务(Huawei Mobile Services,简称 HMS)是一个全面开放的移动服务生态系统,为企业和开发者提供了丰富的工具和 API,助力他们构建、运营和推广应用。其中,HMS Scankit 是华为推出的一款扫描服务 SDK,支持快速集成到安卓应用中,能够提供高效且稳定的二维码和条形码扫描功能,适用于商品扫码、支付验证、信息获取等多种场景。 集成 HMS Scankit SDK 主要包括以下步骤:首先,在项目的 build.gradle 文件中添加 HMS Core 库和 Scankit 依赖;其次,在 AndroidManifest.xml 文件中添加相机访问和互联网访问权限;然后,在应用程序的 onCreate 方法中调用 HmsClient 进行初始化;接着,可以选择自定义扫描界面或使用 Scankit 提供的默认扫描界面;最后,实现 ScanCallback 接口以处理扫描成功和失败的回调。 HMS Scankit 内部集成了开源的 Zxing(Zebra Crossing)库,这是一个功能强大的条码和二维码处理库,提供了解码、生成、解析等多种功能,既可以单独使用,也可以与其他扫描框架结合使用。在 HMS Scankit 中,Zxing 经过优化,以更好地适应华为设备,从而提升扫描性能。 通常,ScanKitDemoGuide 包含了集成 HMS Scankit 的示例代码,涵盖扫描界面的布局、扫描操作的启动和停止以及扫描结果的处理等内容。开发者可以参考这些代码,快速掌握在自己的应用中实现扫码功能的方法。例如,启动扫描的方法如下: 处理扫描结果的回调如下: HMS Scankit 支持所有安卓手机,但在华为设备上能够提供最佳性能和体验,因为它针对华为硬件进行了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值