[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;
}