Uva10404-bachet's game (game, recursive)

Source: Internet
Author: User

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


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.