Brave game
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 6331 accepted submission (s): 4231
Problem descriptionWhen I went to college ten years ago, China introduced some film blockbusters from abroad every year. One of them was called "the player's game" (Name: zathura). Until now, I am still impressed by some of the Computer stunts in the movie.
Today, we chose the computer test as a brave choice. In this short term, we are talking about the game topic. Therefore, now everyone is playing the "multiplayer game", which is why I name this question.
Of course, in addition to being brave, I also hope to see "integrity". No matter what the test scores are, what I want to see is a real result. I believe everyone can do it ~
What is the first game to be played by beginners? It is very simple. It is defined as follows:
1. This game is a two-person game;
2. There are a pile of stones with N in total;
3. Take turns;
4. Each step can take 1... M stones;
5. the first party to win the light stones;
If both sides of the game are using the best strategy, please output which one can win.
InputThe input data first contains a positive integer c (C <= 100), indicating that there are group C test data.
Each group of test data occupies one row and contains two integers n and M (1 <= n, m <= 1000). For the meanings of N and M, see the topic description.
OutputIf a leader can win, output "first"; otherwise, output "second". The output of each instance occupies one row.
Sample input223 24 3
Sample outputFirstsecond
AuthorLcy
Source ACM short term exam_2007/12/13 problem solving: n, m Suppose n <= m, then the first accessors can all at once, the first wins. when N = m + 1, the first party must take at least one, but cannot finish it. As a result, the remaining number is less than or equal to M, so the first party loses. When N = m + 2, the first party can take one, leaving the remaining m + 1 to the latter. The first party wins when n = m + 3, the first can also be left to the latter m + 1, causing the latter to lose. When N = 2 * m-1, the first take the m-2, leave m + 1 to the latter, the first wins when n = 2 * m, if the first take m-1, the remaining m + 1 is given to the latter, and the first wins. If the remainder of N % (m + 1) is not 0, the first win.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 16 int main(){17 int t,n,m;18 scanf("%d",&t);19 while(t--){20 scanf("%d%d",&n,&m);21 if(n%(m+1) == 0) puts("second");22 else puts("first");23 }24 return 0;25 }
View code