解题报告——Block Game(思维题)(Benelux Algorithm Programming Contest 2016 Preliminary)(计蒜客)

Posted 来老铁干了这碗代码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解题报告——Block Game(思维题)(Benelux Algorithm Programming Contest 2016 Preliminary)(计蒜客)相关的知识,希望对你有一定的参考价值。

Description

You are attending the International Construction by Preschoolers Contest. Unfortunately,you are too old to participate, but you still enjoy watching the competition.In between rounds, you are walking around the contest area when you see a toddler, one ofthe contestants, playing with her blocks. Annoyed that she is having all the fun, you decideto challenge her to a game.You set up two stacks of blocks of a certain height. Then, you and the toddler take turnsremoving some number of blocks from the stack which contains the largest number of blocks(if both stacks have the same number of blocks, the current player can choose either stackto remove blocks from). The number of blocks removed must be a positive multiple of thenumber of blocks in the smaller stack. For instance, if there is a stack with 5 blocks, and onewith 23 blocks, then the current player can remove 5, 10, 15 or 20 blocks from the stack of23 blocks. The player who empties one of the stacks wins the game.You have graciously decided to take the first move, but then a worry strikes you – might thisdevious preschooler still be able to beat you?

Input

One line with two integers N and M, satisfying 1 <= N,M <= 10^18, the initial sizes of the twostacks of blocks.

Output

Output a single line containing a single word: the word “win” if you are guaranteed to win if you play correctly, and the word “lose” if your opponent can force you to lose.


心路历程

卡了四个小时,成功AC,喜悦的心情无以言表

题意:甲乙玩游戏, 输入两个数, 二人轮着操作,规则是每次操作只能将较大值减去成倍的较小值,直到一个数为0,从甲开始,二者都使用最佳策略,预测甲的输赢。

本题可以模拟出三种情况:
1、若 a%b=0 则直接win
2、若(b<a<=2*b),则a和b的下一轮值只能为(b,a-b)
3、若(a>=2*b),如果a%b=1,则甲将a和b改为(a%b+b, b)。
如果a%b!=1,则甲将a和b改为(a%b, b),这样,无论如何甲一定赢。

综上所述,只需判断case2即可。

注意:用long long解题。


代码

#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;

void Process(ll a, ll b, ll sum) 
	if((a % b == 0) || (a/b >= 2))  cout << "win" <<endl; return; //若为case1 & case3,直接win
	else 	//判定case2
		while(b!=1) 
			ll t=b; b=a-b; a=t;
			sum++;
			//case2中会出现case3的情况, 如13和10
			if(a/b >= 2)  cout << (sum%2==1?"lose":"win")<<endl; return; 
			//b=1时,意味可以整除,则输出。输出时判定sum
			if(b == 1) 
				cout<<(sum%2==1?"lose":"win")<<endl;
				return;
			
		


int main() 
	ios::sync_with_stdio(false);
	ll a, b;
	cin>>a>>b;
	if(b>a) swap(a,b);
	Process(a, b, 0);
	return 0;
 
全网首发 ,点个赞吧!多谢。

以上是关于解题报告——Block Game(思维题)(Benelux Algorithm Programming Contest 2016 Preliminary)(计蒜客)的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode] 292. Nim Game 解题报告

LeetCode 292 Nim Game 解题报告

LeetCode --- 1025. Divisor Game 解题报告

LeetCode --- 1025. Divisor Game 解题报告

Coloring Game(思维题)

LeetCode --- 1275. Find Winner on a Tic Tac Toe Game 解题报告