nyoj 886 take stones (eight) (Wyzhov game)

Take stones (8)

Time Limit: 1000  ms | Memory Limit: 65535  KB
Difficulty: 3
describe

There are two piles of stones, the number of which can be different. The game begins with two people taking turns taking stones. The game stipulates that there are two different ways to take each time. One is to take as many stones as you want from any pile; the other is to take the same number of stones from two piles at the same time. In the end, the one who removes all the stones is the winner. Now given the initial number of two piles of stones, if it is your turn to take first, assuming both sides take the best strategy, ask whether you are the winner or the loser in the end. If you win, how do you take the first seed? 

enter
The input contains several lines representing the initial conditions of several kinds of stones, each of which contains two non-negative integers a and b, representing the number of two piles of stones, both a and b are not greater than 1,000,000. a=b=0 to exit.
output
The output also has several lines. If you are the loser in the end, it is 0. Otherwise, output 1, and output the number of two piles of stones left after you took the stone for the first time, x,y,x<=y. . If it is possible to win by taking stones from any pile and taking the same number of stones from two piles at the same time, output the case of taking the same number of stones first. The output is taken from a pile with many stones, and the output result is required to ensure that the second value is not less than the first value.
sample input
1 2 
5 7
2 2
0 0
Sample output
0
1
3 5
3 5
4 7
1
0 0
1 2

#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
intmain()
{
	int a,b,t;
	//The judgment is easy, the output is a bit complicated
	double p=((sqrt(5.0))+1.0)/2;//Wyzhov game formula
	while(~scanf("%d%d",&a,&b),a,b)
	{
		if(a>b)
		swap(a,b);
		int s=b-a;
	    int k=floor(s*p);
	    if(k==a)//Determine whether it is a winning situation
	        printf("0\n");
	    else
	    {
	    	printf("1\n");
	    	if(ak==b-(k+s)&&a>=k)//Take the same number of stones from the two piles Note that both conditions must be written, otherwise WA
	    	    printf("%d %d\n",k,k+s);
	    	for(int i=0; ;i++)//Take from the pile
	    	{
	    		int temp=i*p;
	    		if(temp>=b)
	    		    break;
	    		if(b>temp+i&&a==temp)//Take stones from b, and after taking b is bigger than a
	    		    printf("%d %d\n",temp,temp+i);
	    		else if(a==temp+i)//Take stones from b, but after taking b, it is smaller than a
	    		    printf("%d %d\n",temp,temp+i);
	    		else if(b==temp+i&&a>temp)//Take from a, after taking it, make sure that a is still less than b
	    		    printf("%d %d\n",temp,temp+i);
	    	}
	    }
	}
	return 0;
}
	

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939491&siteId=291194637