算法——Euclid's Game

題目描述

Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the board a positive number equal to the difference of two numbers already on the board; this number must be new, i.e., different from all the numbers already on the board. The player who cannot move loses the game. Should you choose to move first or second in this game?

According to the above rules, there are two players play tihs game. Assumptions A write a number on the board at first, then B write it.

Your task is write a program to judge the winner is A or B.

輸入

Two unequal positive numbers M and N , M>N (M<1000000)

輸出

A or B

樣例輸入

3 1

樣例輸出

A

代碼

#include<iostream>
using namespace std;
int main(){
	int n, m;
	cin>>n>>m;
	char win = 'B';
	if(n % (m + 1) != 0){
		win = 'A';
	}
	cout<<win<<endl;
	return 0;
} 

思路

1.題沒有說明白 M ,N 具體代表什麼,其中M : 兩個人誰先寫到這個數,則勝利,N :新寫的數,對於前一個數來說,不得超過的範圍。
2.題很簡單,掌握其中的規律即可:當且僅當M 不是 N+1 的倍數,對先手來說則是一個勝局。
3.勝利的策略是在每次拿走 M mod (N+1) 個棋子;背離這個策略,則會把勝局留給對方。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章