poj2348(博弈)

poj2348

给定两个数a,b,大的数能减少小的数的倍数,不能是的数小于0,谁先使得数等于0,谁就赢了

有三种情况

① a % b ==0  这个状态是必胜的

② a - b < b  这个状态是必胜还是必败,关键在于下一个状态是必胜还是必败

③ a - b > b 这个状态一定是必胜的,这个状态可以看做是a - xb < b 如果a-(x-1)b是比败的,那么a-xb是必胜的,  如果a-(x-1)b是必胜的,那么a-xb是必败的

所以第三种状态一定是必胜的。

所以谁先走到第一种和第三种状态,谁就是必胜的。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <string>
12 #include <math.h>
13 using namespace std;
14 #pragma warning(disable:4996)
15 typedef long long LL;
16 const int INF = 1 << 30;
17 /*
18 2006
19 35357670
20 
21 */
22 
23 int main()
24 {
25     int a, b;
26     while (scanf("%d%d", &a, &b))
27     {
28         if (a == 0 && b == 0)
29             break;
30         bool f = true;
31         for (;;)
32         {
33             if (a < b) swap(a, b);
34             if (a%b == 0) break;
35             if (a - b>b) break;
36             a -= b;
37             f = !f;
38         }
39         if (f)
40             puts("Stan wins");
41         else
42             puts("Ollie wins");
43     }
44 }
原文地址:https://www.cnblogs.com/justPassBy/p/4531584.html