HDU 1525 Euclid ' s game (game)

Source: Internet
Author: User
Tags time 0
Test instructions: The topic gives two positive numbers a, B, each operation can reduce the large number of small number of integer times. When a number becomes 0, it ends.


Who first reduced one of the numbers to a 0 win. Ask who can win. Stan is the initiator.


Idea: Suppose a>=b, if a%b = = 0, then certainly is the initiator wins.  (Click Open link) if a>=2*b. Then that person must know whether a%b,b is winning or losing.

State. If it is a fail state, the initiator will be a A, a a%b,b, then the initiator must win. If it is a winning state, the initiator will change a, b into a%b+b,b. Then the opponent only turns these two numbers into

A%b,b, the initiator wins. If it is b<a<2*b then there is only one way: to become a-b,b (this time 0<a-b<b). So go on and see who is going to face the top of the winning state. The

In the face of B < A <2*b state, the first step to go down. Until the face of a a%b==0 | | The state of a >=2*b.


Code:

Import Java.util.Arrays;
Import Java.util.Scanner;

public class Main {public

	static void Main (string[] args) {
		Scanner sc = new Scanner (system.in);
		int a, B;
		while (Sc.hasnext ()) {
			a = Sc.nextint ();
			b = Sc.nextint ();
			if (a = = 0 && b = = 0) break
				;
			if (a < b) {
				int t = A;
				A = b;
				b = t;
			}
			int win = 0;
			while (b! = 0) {
				if (a = = B | | A >= 2 * b) break
					;
				A-= B;
				int t = A;
				A = b;
				b = t;
				Win ^= 1;
			}
			System.out.println (win = = 0?) "Stan wins": "Ollie wins");}}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.