Bash Game---Normal Edition

Topic 1: Submit link

Topic 1:

There are n stones in a pile. Two people take turns in AB, and A takes first. Every time you get at least 1 stone and at most m, the person who gets the last stone wins. Assuming that AB is very smart, there will be no mistakes in the process of picking stones. Given n and m, ask who will win the game in the end. For example, n = 3, m = 2. No matter how A gets it, B can get the last stone.

Input
line 1: A number T, which represents the number of numbers used for input testing later (1 <= T <= 10000). Lines 2 to T+1: 2 numbers N and K in each line, separated by a space (1 <= N, K <= 10^9).

There are
T rows of Output . If A wins, output A, and if B wins, output B.

Sample Input
4
3 2
4 2
7 3
8 3

Sample Output
B
A
A
B

Analysis of the train of thought:
If 1<=n<=m, the first A will win.
If n==m+1, no matter how many A firsts are taken, B will definitely be finished in the end.
Therefore: m+1 is the key Number of

n satisfies the formula:
n=k*(m+1)+r, (k is a natural number, r is the remainder, 0<=r<=m)

Method: The
first player takes r away first, and always leaves the opponent with stones that are multiples of (m+1), so that he can definitely win.

Code:

#include "stdio.h"
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m;
		scanf("%d %d",&n,&m);
		if(n%(m+1)!=0)
			printf("A\n");
		else
			printf("B\n");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/Helinshan/article/details/110309587