Stone game
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3562 accepted submission (s): 1789
Problem description has two piles of stones, which can be any number and can be different. The game started with taking stones in turn by two people. The game stipulates that there are two different methods to get each time. One is to remove any number of stones from any pile; the other is to take the same number of stones from both piles. Finally, the winner of all the stones. Now we will give you the initial number of two stones. If it is your turn to take them first, let us assume that both Parties adopt the best strategy and ask whether you are the winner or the loser at last.
The input contains several rows, indicating the initial conditions of several stones. Each row contains two non-negative integers A and B, indicating the number of two stones. both A and B are not greater than 1,000,000,000.
The output also has several rows. Each row contains a number 1 or 0. If you are the winner at the end, it is 1. Otherwise, it is 0.
Sample input2 18 44 7
Sample output010
Wythoff game ):There are two stacks of items, and two people take turns from one pile or the same
Get the same number of items from the two stacks. You must get at least one item each time.
In this case, it is quite complicated. We use (AK, BK) (Ak ≤ BK, K = 0, 1, 2 ,..., N) indicates
The number of two heaps of items is called a situation. If a faces (0, 0), a has lost. In this situation, we
It is called a singular situation. The first few strange situations are: (0, 0), (1, 2), (3, 5), (4, 7), (6,
10), (8, 13), (9, 15), (11, 18), (12, 20 ).
It can be seen that a0 = b0 = 0, AK is the minimum natural number that has not appeared before, and BK = ak + K,
The singular situation has the following characteristics:
1. Any natural number is contained in one and only one singular situation.
Because AK is the minimum natural number that has not appeared before, so there is a [k]> A [k-1], BK = A [k] + k> A [k-1] + k> A [k-1] + k-1 = B [k-1]> A [k-1]. So property 1 is true.
2. Any operation can change a singular situation to a non-singular situation.In fact, if only one component of the singular situation (AK, BK) is changed, the other component cannot be in another singular situation, so it must be a non-singular situation. If the two components of (AK, BK) are reduced at the same time, it is a non-singular situation because the difference remains unchanged and cannot be the difference of other singular situations.
3. An appropriate method can be used to convert a non-singular situation into a singular situation.Assume that the situation is (A, B). If B = A, A is taken from both heaps to a strange situation ); if a = ak, B> BK, then the objects B-BK are taken away, which becomes a singular situation. If a = ak, B <BK then simultaneously removes a-a [B-A] objects from the two heaps to a singular situation (A [B-A], b-A + A [B-A]). If a> AK, B = ak + K, the excess number of A-ak can be removed from the first heap; if a <AK, B = ak + k, there are two situations: first, a = AJ (j <k) Takes B-BJ from the second heap; second, A = BJ (j <k) Just remove B-AJ from the second heap.
From the above nature, we can see that if both of them adopt correct operations, the first winner will win in the face of non-singular situations, and the second winner will win.
How can we determine whether a situation (A, B) is a singular situation? We have the following formula:
Ak = [K (1 + √ 5)/2], BK = ak + K (k = 0, 1, 2 ,..., N square brackets indicate the entire function)
What's amazing is that there is a golden split (1 + √ 5)/2 = 1. 618 ..., Therefore, the rectangle composed of AK and BK is near
It is like a golden rectangle. Because 2/(1 + √ 5) = (√ 5-1)/2, you can first find J = [A (√ 5-1) /2]. If a = [
J (1 + √ 5)/2], then a = AJ, bj = AJ + J. If not, then a = AJ + 1, BJ + 1 = AJ + 1
+ J + 1. If none of them are, it is not a singular situation. Then, according to the above rules, we will surely encounter a strange situation.
Situation.
1 # include <iostream> 2 # include <cstdio> 3 # include <algorithm> 4 # include <string> 5 # include <cstring> 6 # include <cmath> 7 using namespace std; 8 int A, B; 9 int main () {10 while (~ Scanf ("% d", & A, & B) {11 int temp; 12 if (A> B) {13 temp = A; 14 A = B; 15 B = temp; 16} // ensure that B is greater than a 17 int K = B-A; 18 int ak1 = int (K * (1 + SQRT (5.0 )) /2.0); // note that the floating point number is 19 if (ak1 = A) printf ("0 \ n"); // It is a strange situation, enter 20 else printf ("1 \ n"); 21} 22 23 return 0; 24}