It seems like the game is over! (Dynamic Planning ),
Source of https://biancheng.love/contest-ng/index.html#/41/problems
F
It seems like the game is over!
Description
Nova is in trouble because he cannot beat his opponent by operating on the PS4 game. He had to play with his opponent!
The solution proposed by nova is somewhat different from the previous one. He said: "You can decide the number of stones and the method of learning stones. It is very fair for me to decide the sequence ." His opponent thinks that what nova says makes sense, so it not only determines the number of stones in each game, but also gives k numbers, it indicates that the number of stones that can be taken at any time is equivalent to one of the k numbers, and one of k must be 1. The winner is the winner.
Now it's very urgent. Let's help nova to see if he should be able to beat his opponent first or later.
Input
There are two rows of test data in each group.
The first line has two integers n and k, and the second line has k integers, meaning as described in the topic.
N <= 100000, k <= 15
Output
For each group of data, output a row, which is the successive sente \ gote that nova should adopt.
Input example
4 31 2 31 11
Output example
Gotesente
Solution: given n stones and k stone removal methods, each method can remove a certain amount of stones. Now Sente (S), gote (O ), S first hand, O later hand, each time each person can choose a way to remove the Stone, who removed the last pile of who will win. Determine who is the winner.
Analysis:
1. Use a dp array for record. The value of the record obtained by the First operator is 1, and that of the latter is 0.
2. the initial dp array is 0, traversing 1 to n. If dp [I] is 0, it indicates that the previous one is obtained by the latter hand, so that the first hand can be retrieved and the dp [I] is changed to 1, since it is from 1 to n, it is feasible to record the previous state records.
3. In this way, you only need to determine whether dp [n] is 1 or 0 to determine whether it is a first-hand victory or a second-hand victory.
The state transition equation is: if (I-mjmj [j]> = 0 &&! Dp [I-mjmj [j]) dp [I] = 1.
Code:
1 #include <bits/stdc++.h> 2 #define MAX 1000010 3 int dp[MAX],mjmj[15]; 4 5 int n,m,i,j; 6 int main() 7 { 8 while(~scanf("%d",&n)) 9 {10 memset(dp,0,sizeof(dp));11 scanf("%d",&m);12 for (i=0; i<m; i++)13 {14 scanf("%d",&mjmj[i]);15 }16 for (i=1;i<=n;++i)17 {18 for (j=0;j<m;++j)19 {20 if (i-mjmj[j]>=0&&!dp[i-mjmj[j]])21 {22 dp[i]=1;23 break;24 }25 }26 }27 if (dp[n])28 printf("sente\n");29 else30 printf("gote\n");31 }32 return 0;33 }
Recommended blog: http://blog.csdn.net/kjc19920531/article/details/8120989
Recommended blog: http://blog.csdn.net/wangtaoking1/article/details/7308117