Problem Solving Report-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.


Mental journey

After being stuck for four hours, successfully AC, the joy is beyond words

Question: A and B play the game, enter two numbers, and the two will operate in turn. The rule is that each operation can only subtract the larger value from the smaller value, until one number is 0. Starting from A, both Both use the best strategy to predict A's win or loss.

This question can simulate three situations:
1. If a%b=0, then win directly
2. If ( b<a<=2*b), then the next round of a and b can only be (b,a-b)
3, if ( a>=2*b), if a%b=1, then A will a and b Change to ( a%b+b, b).
If a%b!=1, then A changes a and b to ( a%b, b), so that A must win anyway.

In summary, only case2 needs to be judged.

Note: Use long long to solve the problem.


Code

#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;
} 
Started on the whole network, please like it! Thanks a lot.

Guess you like

Origin blog.csdn.net/weixin_43899069/article/details/108423791