P1290 Euclidean game, p1290 Reid
Description
Two descendants of Euclidean Stan and Ollie are playing a digital game invented by their ancestor Euclidean. Given two positive integers M and N, starting from Stan, from a larger number, minus a positive integer multiple of a smaller number, of course, the obtained number cannot be less than 0. Then there is Ollie. perform the same operation on the obtained number, which is smaller than M and N ...... Until a person gets 0, he wins. The following is the process of using () two number games:
Start: 25 7
Stan: 11 7
Ollie: 4 7
Stan: 4 3
Ollie: 1 3
Stan: 1 0
Stan won the game.
Now, if they operate perfectly, who will win?
Input/output format:
The number of group C for the first behavior test data. There are rows C below, each row is a group of data, including two positive integers M, N. (M, N cannot exceed long integer type .)
Output Format:
Output a row of input data for each group. If Stan wins, "Stan wins" is output; otherwise, "Ollie wins" is output"
Input and Output sample input sample #1:
225 724 15
Output sample #1:
Stan winsOllie wins
Set whether to win the first hand to d (a, B). Set a> = B.
It can be listed as follows:
When a-B> B, d (a, B) = (not d (a-B, B) or (not d (a-2b, B) or... or (not d (a mod B, B )),
At this time there is d (a-B, B) = (not d (a-2b, B) or (not d (a-3b, B) or... or (not d (a mod B, B )),
D (a, B) = (not d (a-B, B) or d (a-B, B) = true.
When a-B = B, it is clear that d (a, B) = true.
When a-B <B, if a> B has only one decision, that is, d (a, B) = not d (B, a-B ), if a = B, d (a, B) = true.
You can change Recursion to a loop (which is actually used to simulate the strategy of each agency) (Data water, or you can leave it unchanged ).
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 const int MAXN=5001; 9 void read(int &n)10 {11 char c='+';int x=0;bool flag=0;12 while(c<'0'||c>'9')13 {c=getchar();if(c=='-')flag=1;}14 while(c>='0'&&c<='9')15 {x=x*10+c-48;c=getchar();}16 flag==1?n=-x:n=x;17 }18 int main()19 {20 int T;21 read(T);22 while(T--)23 {24 int x,y;25 read(x);read(y);26 int now=1;27 if(x<y)swap(x,y);28 while(1)29 {30 if(x==y||x-y>=y)31 break;32 now=!now;33 int tmp=x-y;34 x=y;35 y=tmp;36 }37 if(now)38 printf("Stan wins\n");39 else 40 printf("Ollie wins\n");41 }42 43 return 0;44 }