T- Fetch (2 stacks) Stone gameTime
limit:MS
Memory Limit:32768KB
64bit IO for Mat:%i64d &%i64u Submit Status Practice HDU 2177
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. If you win, how do you take your son for the 1th time?
Input
The input contains several lines that represent the initial condition of several stones, each of which contains two nonnegative integers a and B, indicating the number of two stones, A and b are not greater than 1,000,000, and a<=b. A=b=0 exit.
Output
The output also has several lines, if you are the loser at the end, then 0, the opposite, the output 1, and the output you win the 1th time you take the stone after the remaining two piles of stone x,y,x<=y. If you take a stone in any heap, you can win the same number of stones simultaneously in both piles, and output the same number of stones first.
Sample Input
Sample Output
Reprint Please specify the Source: Looking for Children & stars
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2177
About Witzov Game there is another piece: http://blog.csdn.net/u010579068/article/details/47306029
Idea: First of all P-points to hit the table, and then if the P-point so direct output 0;
If it is n point, then the first match is subtracted and then the P point;
Then divided into three kinds of situation discussion: (n,m) -----> Title Guarantee N<m, Pit!
1. If Px==n && py<m
2. If Py==m && px<n
3. If Py==n && px<m
Note: The same situation, the small first output eg (n,m) = (5,8)------> (3,5)
The first few strange situations are://(0,0), (3,5), (4,7), (6,10),//(8,13), (9,15), (11,18), (12,20)//Law: hypothesis (x, y) and x<y; Then x must be the smallest positive integer that is not present,//while y=x+k;//the table first #include<stdio.h> #include <math.h>struct stone{int x; int y;} Num[400000];bool vis[1000005]= {false};int cnt;void init () {int k=1; num[0].x=0; num[0].y=0; Vis[0]=true; for (int i=1;; i++) {if (!vis[i]) {num[k].x=i; Num[k].y=i+k; Vis[num[k].x]=true; Vis[num[k].y]=true; k++; } if (num[k-1].y>1000000) break; } cnt=k;//number}void Print (int x,int y) {//printf ("n=%d,m=%d,t=%d\n", X,Y,TT); int x1,x2,y1,y2; x1=x2=y1=y2=0; for (int i=0; i<cnt; i++) {if (num[i].x<x) {if (X-NUM[I].X==Y-NUM[I].Y) { printf ("%d%d\n", num[i].x,num[i].y); }} if (Num[i].x==x&&num[i].y<y) {x1=num[i].x; Y1=NUM[I].Y; } if (Num[i]. y==y&&num[i].x<x) {x1=num[i].x; Y1=NUM[I].Y; } if (num[i].y==x&&num[i].x<y) {//There is no need to exchange x2=num[i].x here; Y2=NUM[I].Y; } else if (num[i].x>y) break; } if (x1+y1!=0) printf ("%d%d\n", x1,y1); if (x!=y&&x2+y2) printf ("%d%d\n", x2,y2);} int main () {int n,m,k,t; Init ();//printf ("cnt=%d\n", cnt);//printf ("%d%d\n", num[cnt-1].x,num[cnt-1].y); while (scanf ("%d%d", &n,&m), n+m) {//title guaranteed n<m int kt=0; K=m-n; T=k* (1+SQRT (5))/2; if (t==n) printf ("0\n"); else {printf ("1\n"); Print (N,M); }} return 0;}
WA took one more time and considered a little more.
#include <stdio.h> #include <math.h>struct stone{int x; int y;} Num[400000];bool vis[1000005]= {false};int cnt;void init () {int k=1; num[0].x=0; num[0].y=0; Vis[0]=true; for (int i=1;; i++) {if (!vis[i]) {num[k].x=i; Num[k].y=i+k; Vis[num[k].x]=true; Vis[num[k].y]=true; k++; } if (num[k-1].y>1000000) break; } cnt=k;//number}void Print (int x,int y,int TT) {//printf ("n=%d,m=%d,t=%d\n", X,Y,TT); int x1,x2,y1,y2; x1=x2=y1=y2=0; for (int i=0; i<cnt; i++) {if (num[i].x<x) {if (X-NUM[I].X==Y-NUM[I].Y) { if (TT) printf ("%d%d\n", num[i].y,num[i].x); else printf ("%d%d\n", num[i].x,num[i].y); }} if (Num[i].x==x&&num[i].y<y) {if (TT) {x1=num[i]. Y y1=num[i].x; } else {x1=num[i].x; Y1=NUM[I].Y; }} if (Num[i].y==y&&num[i].x<x) {if (TT) {x1=num[i]. Y y1=num[i].x; } else {x1=num[i].x; Y1=NUM[I].Y; }} if (Num[i].y==x&&num[i].x<y) {if (TT) {x2=num[i]. X Y2=NUM[I].Y; } else {x2=num[i].y; y2=num[i].x; }} else if (num[i].x>y) break; } if (x1+y1!=0) printf ("%d%d\n", x1,y1); if (x!=y&&x2+y2) printf ("%d%d\n", y2,x2);//no swap, Pit! }int Main () {int n,m,k,t; Init ();//printf ("cnt=%d\n", cnt);//printf ("%d%d\n", num[cnt-1].x,num[cnt-1].y); while (scanf ("%d%d", &n,&m), n+m) {int kt=0; if (n>m)//ACThe value of the change n,m. Make n>m; {n^=m; M^=n; N^=m; Kt=1; } k=m-n; T=k* (1+SQRT (5))/2; if (t==n) printf ("0\n"); else {printf ("1\n"); Print (N,M,KT); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Take (2 piles) Stone game (Witzov game +hdu2177)