Game Problem-alice and Bob's card game

Source: Internet
Author: User

Descriptionbob and Alice play a game, and Bob would play first. This is the rule of the game:1) there was N stones at first;2) Bob and Alice take turns to remove stones. Each time, they can remove p^k stones. P is prime number, such as 2, 3, 5, ..., and K is a non-negative integer.3) The one who removes the last stone wins the G Ame. You had to tell Bob, if both Alice and Bob play perfectly, who would win the game. Inputthe first line of the input is a positive integer T (1<t<=10000). T is the number of the "test case followed". Each test case contains one line with a integer N (1<n<1000000) which is the number of the stones at the beginning. Outputthe output of each test case is only contains the name of the winner: "Bob" or "Alice". No redundant spaces is needed. Sample input Copy sample input to Clipboard
256
Sample Output
Bobalice
The general meaning is that Alice and Bob play games, there are n cards, each person can only take a number of power of a prime, Bob First, then take turns, and then take the lastthe root of the win, given a number n, judged in the best strategywho wins next.       This problem seems a little complicated, in fact, as long as the simplest case, the deduction can be derived from the answer, the following steps:   1. Obviously, when n=1,2,3,4,5 is Bob wins, when N=6 Alice wins, here the 6 is called "Bad number", because who takes it who loses;     2. Then Bob wins when n=6+1,2,3,4,5=7,8,9,10,11, because at this point Bob just takes 1,2,3,4,5 separately to make the remaining 6, 6This bad number is thrown to Alice, so Alice loses;and when the n=6+6=12 can not be done, because at this time the smallest and only less than 12 "bad number" is 6, and Bob is obviously unable to take 6 (bad number) card. 3. When n=12+1,2,3,4,5=13,14,15,16,17, Bob wins, too, because Bob can throw 12 of the "bad" to the 1,2,3,4,5 if he takes the other one separately.Alice (previously deduced that 12 is a "bad number"(who loses)); When N=12+6=18, Bob couldn't get 12, 6 (bad numbers), leavingthrow the bad numbers to Alice for 6,12, and once this is done, he can only throw Alice a "good number" inafter that, Alice either takes the rest of the cards straight, if not.can also produce a bad number to throw to Bob, no matter how Bob will lose.     In fact, there is a point here, how to prove that Alice can not directly take the rest of the cards, also must be able to produce a bad number to throw Bob and make Bob lose? Because from thefront 6,12,18 can be seen in 1-18, only "bad numbers"It is not possible to generate a power of minus prime numbers to produce a bad number (so it loses), and "good numbers" are certain to produce bad numbers .(so only win), when the n>18 is the same as proof (after the article has a mathematical induction method to prove).    4. Believe that you have seen here, n=18+1,2,3,4,5=19,20,21,22,23, is also a "good number", Bob will win, N=18+6 can not produce badcount, and Bob will lose again, until here we have been able toSee, a multiple of 6 is a bad number, and the bad number is a multiple of 6 even if you do not believe, we can at least hand calculate 6,12 thisTwo bad number is a multiple of 6, then by the mathematical induction, assuming n=6m, when M<=k is a multiple of 6 is the bad numberand the bad number is a multiple of 6, then when M=k+1,N for a good number (that is, to overturn my conclusion), it is time to n=6 (k+1) to lose a good number into a bad number, from the previous assumption, because the previous bad number is 6 timesnumber, then it can only lose a multiple of 6 to produce a multiple of 6 (because he is a multiple of 6), and previously assumed that a multiple of 6 is a bad number, so it is not reduced,evidence. (a bit verbose, please Haihan)Here we can conclude that if (n%6), Bob wins, else Alice wins (it's a sad conclusion). Summary: 1. In fact, from the above deduction can be seen, all the number is divided into two sets: A (good number) and B (bad number), a set of the number of the following characteristics: can be taken awayor can take away some of the number after generating a B set;the number of B collections has the following characteristics: It's impossible to take and take some numbers at a time. Only the number of a set can be generated, not the B set .number (nested, somewhat difficult to understand). Just so, if Bob gets the B number at first, at least this round isn't going to be right .win, and Alice must be given a number a, and Aliceget a number even if you can't get the light at once, you can produce again.give birth to a B number, so Bob can't win, and Alice can keep doing it so Bob can't win untilAlice gets a number A that can be picked up at a time.win! If Bob gets a number A at first, he can do the same thing as Alice does until he wins, whichis throughThe above inductive pointsanalysis of the best strategy! 2. In fact, more than a few game problems will find that the best strategy is to let the other side "have to do", and we can always let the other side into the "have to do" situation,this will win! Further induction, game problem general situation can be divided into two kinds: "Have to do" situation, can let the other side into the "have to do" situation,as soon as the player findsthese two forms of demarcation (this is the most difficult is also the core), can be seen in the game start can see oneself in that kind of situation, thus invincible! 3. When I first saw the problem, I always put the emphasis on Bob to get it, even if he knew what Bob did, but felt Alice's behavior was uncertain, soit feels too hard to do, but as said above, the optimal strategy of game problem is to eliminate the uncertainty of the other's behavior, so that the other party has to do; and always tangled in the "power of mass", the result of mass power is completely useless, but from the 1,2,3,4,5,6 of the most basic number can deduce the conclusion! 4. As you can see from this question,the two sides of the game are following the same rules, so they are trying to work in the same direction, so they don't have to struggle with who firstwho, after all, did not have to dwell ontwo players, see only one player, and sometimes not even too entangled in the rules (such as the question of the number of power), if you do not know how to do self -must win, thencan change the angle, as long as the ways toThe square loses, if still feel unableHands , butto start with the simplest, and then deduce, the induction may be a new discovery!     (the first time to write a blog, some wordy, please Haihan)

Game Problem-alice and Bob's card game

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.