Hdu1847 good luck in CET-4 everybody! , Bashi game, understanding SG Functions

Source: Internet
Author: User

Hdu1847 good luck in CET-4 everybody!

Question:

There are n cards in total, and both parties take turns to capture cards. The number of cards each time can only be 2 power times (I .e., 16 ...), After the card is captured, the result of the victory is also displayed: the winner of the card is the final one. Give N. Can I ask if I win first or later?

PS: of course, this question can be launched directly n % 3 = 0 to defeat, otherwise it will win. // Bashi game



The following introduces another method.

SG value: the SG value of a vertex is the smallest integer greater than or equal to zero that is not equal to the SG value of its successor vertex. // Same as the Mex () function

Simply put, it is the distance from the current status to the nearest point of failure.


SG (x) = Mex {SG (s )}
S is a set of SG function values in the subsequent State of X. Mex (s) indicates the minimum non-negative integer that is not in S.
SG (x) = 0 if and only if X is defeated.

Let's enumerate the SG values with a card number of 0 to 10:
Num: 0 1 2 3 4 5 6 7 8 9 10
SG value: 0 1 2 0 1 2 0 1 2 0 1

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 1000 + 10; int arr [11], SG [maxn]; void pre () {// calculate all the cards that may be taken within 1000 at a time! Arr [0] = 1; for (INT I = 1; I <= 10; ++ I) Arr [I] = arr [I-1] * 2 ;} int Mex (int x) {// This is an algorithm function for solving the SG value of this point (using a memory search) if (SG [x]! =-1) return SG [X]; bool vis [maxn]; memset (VIS, false, sizeof vis); For (INT I = 0; I <10; ++ I) {int temp = x-Arr [I]; If (temp <0) break; SG [temp] = Mex (temp ); vis [SG [temp] = true;} For (INT I = 0; ++ I) {If (! Vis [I]) {SG [x] = I; break;} return SG [X];} int main () {int N; Pre (); while (scanf ("% d", & N )! = EOF) {memset (SG,-1, sizeof SG); If (MEX (N) printf ("Kiki \ n "); else printf ("Cici \ n");} return 0 ;}


Hdu1847 good luck in CET-4 everybody! , Bashi game, understanding SG Functions

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.