HDOJ 2999 Stone Game, Why are you always there? Game SG Functions

Source: Internet
Author: User

[Cpp]
// HDOJ 2999 Stone Game, Why are you always there? Game SG Functions
/*
N stones are arranged in a row, and each time only f stones are taken away, f is a given set.
Q: How can I win a game?
 
Idea: the situation cannot be judged.
Assume that there are only one number in four stone sets.
The subsequent statuses include}
Because {} and {} indicate the same status, you can add a pruning rule.

*/
 
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
 
# Define N 105
# Define M 1005
 
Int n, cnt;
Int op [N], sg [M];
 
Int mex (int n ){
Int I, j;
Bool vis [1005];
If (sg [n]! =-1)
Return sg [n];
Memset (vis, false, sizeof (vis ));
For (I = 1; I <cnt & op [I] <= n; ++ I ){
For (j = 0; j + op [I] <= n & j <n/2 + 1; ++ j ){
If (sg [j] =-1)
Sg [j] = mex (j );
If (sg [n-j-op [I] =-1)
Sg [n-j-op [I] = mex (n-j-op [I]);
Vis [sg [j] ^ sg [n-j-op [I] = true;
}
}
For (I = 0; ++ I)
If (vis [I] = false)
Return I;
}
 
Int cmp (const void * a, const void * B ){
Return * (int *) a-* (int *) B;
}
 
Int main (){
Int I, k, m;
While (scanf ("% d", & n )! = EOF ){
For (I = 1; I <= n; ++ I)
Scanf ("% d", & op [I]);
Qsort (op + 1, n, sizeof (op [0]), cmp );
Cnt = 2;
For (I = 2; I <= n; ++ I)
If (op [I]! = Op [I-1])
Op [cnt ++] = op [I];
 
Memset (sg,-1, sizeof (sg ));
Scanf ("% d", & k );
While (k --){
Scanf ("% d", & m );
If (sg [m] =-1)
Sg [m] = mex (m );
Puts (sg [m]? "1": "2 ");
}
}
Return 0;
}

Related Article

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.