Hdu 4111 Alice and Bob (game)

Source: Internet
Author: User

Hdu 4111 Alice and Bob (game)

Link: hdu 4111 Alice and Bob

Alice and Bob play games with N piles of stones. Each time they take a rock from a pile or merge the two stones, Alice first asks

The final winner.

Solution: NP theorem, write some NP theorem to find the law, and then use the inductive method to prove it.

Count the number c of 1 and the number s of steps in the case of not 1, including merging.

  • C is an odd number, and s is not equal to 2: The first hand wins.
  • S is a multiple of 2 or 0: c is 3.
  • Otherwise, if s is an odd number, the first hand wins.

    If there is no 1 in the first place, it is proved that when the total number of steps is an odd number, the first hand wins, N points. On the contrary, if it is an even number, it is a pvertex. (In this case

    To win the next game, you only need to ensure that the number of any pile of stones is no less than 2 until there is only one pile at the end, because 1 is a special one)

    In addition, it is proved that an odd number of 1 and s is not 2, it is a winning state. Starting from a 1, suppose there is a 1 and s. If s is an odd number, it can be merged first.

    Two piles of stones; if s is an even number, the first hand can take 1; so in this case, the first hand must win, because move 1 can choose to drop one or two steps.

    Now there are two cases of 1 and s. If one of them consumes 1 and merges with s, the remaining State is (1, s + 1) N points; remove 1,

    The remaining (1, s) is also N points. merge two 1, (2, s) => (s + 3) States. If s is an odd number, that is, P-state. The first hand is the winner, and vice versa.

    This is a defeat. This situation is summarized into the third article in the conclusion. In the case of three values, one value can be changed to another value.

    However, when s = 2 or s = 0, it is a special case, because after 2 is removed, it becomes 1, and when s is 0, it is all 1. Through the above rule, we already know

    1 is special. Now there is an NP Graph

    It is not hard to find that if c is a multiple of 3, it must be a first-hand defeat.

    #include 
        
         #include 
         
          #include using namespace std;bool judge (int c, int s) {    if (c&1 && s > 2)        return true;    if (s == 0 || s == 2)        return c % 3;    return s&1;}int main () {    int cas;    scanf("%d", &cas);    for (int kcas = 1; kcas <= cas; kcas++) {        int n, s = 0, c = 0, x;        scanf("%d", &n);        for (int i = 0; i < n; i++) {            scanf("%d", &x);            if (x == 1)                c++;            else {                if (s == 0)                    s = x;                else                    s += x + 1;            }        }        printf("Case #%d: %s\n", kcas, judge(c, s) ? "Alice" : "Bob");    }    return 0;}
         
        

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.