HDU4111 UVA1500 LA5760 Alice and BobSG函数+规律

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU4111 UVA1500 LA5760 Alice and BobSG函数+规律相关的知识,希望对你有一定的参考价值。

Alice and Bob
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2540 Accepted Submission(s): 970

Problem Description
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!

Input
The 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 A1 …AN(1 <= Ai <= 1000), represents the numbers they write down at the beginning of the game.

Output
For 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

Source
2011 Asia ChengDu Regional Contest

问题链接HDU4111 UVA1500 LA5760 Alice and Bob
问题简述:Alice和Bob两人玩取石游戏。有N堆石子,每次可以从一堆中取走一个石子,或者合并两堆石子,Alice先,问
说最后谁赢?
问题分析:博弈的SG函数问题,可以用SG函数来解决。也可以找规律来解决,统计1(一堆只有1个石子)的个数cnt和非1情况下的步数sum,若cnt为奇数并且sum不为2则先手胜;若sum为2或0并且cnt为3的倍数则先手必负;其他情况,sum为奇数则先手胜,否则先手负。给出的题解是按规律的题解。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU4111 UVA1500 LA5760 Alice and Bob */

#include <bits/stdc++.h>

using namespace std;

bool judge (int cnt, int s)
{
    if (cnt & 1 && s > 2) return true;

    if (s == 0 || s == 2) return cnt % 3;
    else return s & 1;
}

int main()
{
    int t;
    scanf("%d", &t);
    for (int k = 1; k <= t; k++) {
        int n, sum = 0, cnt = 0, a;

        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &a);
            if (a == 1) cnt++;
            else sum = (sum == 0 ? a : sum + a + 1);
        }

        printf("Case #%d: %s\\n", k, judge(cnt, sum) ? "Alice" : "Bob");
    }

    return 0;
}

以上是关于HDU4111 UVA1500 LA5760 Alice and BobSG函数+规律的主要内容,如果未能解决你的问题,请参考以下文章

UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking大数+进制

Alice and Bob HDU - 4111 (SG函数)

Alice and Bob——hdu4111

UVA557 LA5578 Burger概率

UVA280 LA5588 VertexDFS

UVA557 LA5578 Burger概率