Poj1067-Take the Stone game (Witzov game) "Game"

Source: Internet
Author: User

http://poj.org/problem?id=1067

Take a stone game
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36753 Accepted: 12446

Description

There are two piles of stones, the quantity is arbitrary, can be different. The game began with two people taking turns to take stones. The game rules, each time there are two different ways, one can take away any number of stones in any heap, and two can take the same number of stones in both piles. Finally, the stones are all taken out as the winner. Now give the number of the initial two piles of stones, if it's your turn to take it first, assume that both sides adopt the best strategy and ask whether you are the winner or the loser.

Input

The input contains several lines that represent the initial condition of several kinds of stones, each of which contains two nonnegative integers a and B, indicating the number of stones, A and B, not greater than 1,000,000,000.

Output

The output corresponds to a number of lines, each containing a number 1 or 0, or 1 if you are the winner, or 0 for the other.

Sample Input

2 18 44 7

Sample Output

010

The puzzle: This is Witzov game. The so-called Witzov game, is one of the common combination games in ACM problem, is basically this:
There are two piles of stones, you may think of a pile of 10, another pile of 15, the two sides take some stones in turn, the legal way has the following two kinds:
1, in a heap of stones to take away any number of;
2. Take the same number of free stones in two piles;
Agreed to take the last stone of the man-made winner, to win the strategy.

The two piles of stone status are the same, we use the remaining stone number (A, B) to indicate the state. States have a must, a winning state and a possible victory state. For example: (0,0) must be a must-fail state. (0,k), (k,0), (k,k) series of nodes must be a winning state. After analysis and deliberation, we can know that there are: (0,0), (3,5), (4,7), (6,10), (8,13), (9,15), (11,18), (12,20) and so on. For a must-fail state (AK,BK), we have: AK =[k* (1+√5)/2],bk= AK + K (K=0,1,2,...N, where square brackets represent the rounding function). For the must-fail state, the first to lose, for non-mandatory, the first to win. As for the mathematical proof, refer to a blog friend's article: http://blog.sina.com.cn/s/blog_727d57100100ql6e.html

Code:

1#include <fstream>2#include <iostream>3#include <cstdio>4#include <cmath>5 6 using namespacestd;7 8 intMain ()9 {Ten     //freopen ("d:\\input.in", "R", stdin); One     //freopen ("D:\\output.out", "w", stdout); A     intb; -     DoubleM= (sqrt (5.0)+1)/2; -      while(~SCANF ("%d%d",&a,&b)) { the         if(a<b) { -a^=b; -b^=A; -a^=b; +         } -         intk=a-b; +         if(b== (int) (k*m)) APuts"0"); at         Else -Puts"1"); -     } -     return 0; -}

Poj1067-Take the Stone game (Witzov game) "Game"

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.