Test instructions in order to give the total number of stones N, and M heap of stones, two people round to take stones, each time can only from the total stone in M heap of the number of stones, take away the last stone wins. s take first, o after.
With the game of the idea of winning or losing +DP can be, because the memory of the search will explode stack, then the recursion, simply push ...
Otherwise it would not ...
#include <iostream> #include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <vector> #include <algorithm >using namespace Std;int dp[1000010];int a[20];int n,m;int main () {int I,j;while (cin>>n>>m) {for (i=0;i <m;i++) cin>>a[i];for (i=0;i<=n;i++) {dp[i]=0;for (j=0;j<m;j++) if (i-a[j]>-1&&dp[i-a[j]]= =0) {dp[i]=1;continue;}} if (dp[n]==1) cout<< "Stan wins" <<endl;elsecout<< "Ollie wins" <<ENDL;}
Bachet ' s Game
Time Limit:6666MS |
|
Memory Limit: Unknown |
|
64bit IO Format:%lld &%llu |
Submit Status
Description
Problem B:bachet ' s game Bachet ' s game is probably known to all but probably isn't by this name. Initially there is
NStones on the table. There is players Stan and Ollie, who move alternately. Stan always starts. The legal moves consist in removing at least one and not more than
kStones from the table. The winner is the one-to-take of the last stone.
Here we consider a variation of the This game. The number of stones that can is removed in a single move must is a member of a certain set ofm numbers. Among the m numbers there is always 1 and thus the game never stalls.
Inputthe input consists of a number of lines. Each line describes one game by a sequence of positive numbers. The first number is
N<= 1000000 The number of stones on the table; The second number is
m<= giving the number of numbers that follow; The last
mNumbers on the line specify how many stones can is removed from the table in a single move. Inputfor each line of input, output one line saying eitherStan winsOrOllie winsAssuming that both of them play perfectly. Sample input
20 3 1 3 821 3 1 3 822 3 1 3 823 3 1 3 81000000 10 1 23 38 11 7 5 4 8 3 13999996 10 1 23 38 11 7 5 4 8 3 13
Output for sample input
Stan Winsstan winsollie Winsstan Winsstan Winsollie wins
problem SETTER:PIOTR Rudnicki
Source
Root:: Competitive programming 3:the New Lower Bound of programming contests (Steven & Felix Halim):: Mathematics: : Game theory:: Standard
Root:: AOAPC i:beginning algorithm Contests (Rujia Liu):: Volume 5. Dynamic programming
Root:: Competitive programming 2:this increases the lower bound of programming contests. Again (Steven & Felix Halim):: Mathematics:: Game Theory-standard
Root:: Prominent problemsetters:: Piotr Rudnicki
UVa 10404. Bachet ' s Game