Question: uva10404-bachet's game (game, recursive)
Two friends, Stan and Ollie, are playing stone games: give n stones and then give m numbers. It is required that Stan start at a time and take M (one of the M numbers) stones at a time. After the stone was taken, no stone left on the table won.
Solution: game. For J stones, if Stan wants to win, he will take the J stones odd times.
Assuming that J stones win by Stan, then J + M stones win by Ollie. In this case, let d [J] = 1 represent Stan to win, and 0 represent Ollie to win, As long as J-m can be found to be 0, then Stan can take away each stone and reach the status J-M, causing Ollie to lose the game.
State transition equation DP [I] = DP [I-m] = 0? 1: do not change the DP [I] value;
Note that the DP [I] value must be determined only when DP [I-m] is determined. Therefore, the DP [I-m] value must be calculated first.
Code:
# Include <cstdio> # include <cstring> const int n = 10000005; const int M = 10; int V [m]; int DP [N]; int main () {int n, m; while (scanf ("% d", & N, & M )! = EOF) {for (INT I = 0; I <m; I ++) scanf ("% d", & V [I]); sort (V, V + M); memset (DP, 0, sizeof (DP); // because only one value is assigned, these values can be set to 0 initially, if all d [J-V [I] is determined later ]]! If it is set to 0, it means that Ollie wins and no value is assigned to 0. For (INT I = 1; I <= N; I ++) // ensure that the previous small DP [I] value is determined first. For (Int J = 0; j <m; j ++) {if (I> = V [J] & DP [I-V [J] = 0) DP [I] = 1;}/* For (INT I = 0; I <m; I ++) for (Int J = V [I]; j <= N; j ++) if (DP [J-V [I] = 0) DP [J] = 1; */printf ("% s wins \ n ", DP [N]? "Stan": "Ollie");} return 0 ;}