poj2348(博弈)

时间:2022-06-18 10:09:34

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是必败的

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

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

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = << ;
/*
2006
35357670 */ int main()
{
int a, b;
while (scanf("%d%d", &a, &b))
{
if (a == && b == )
break;
bool f = true;
for (;;)
{
if (a < b) swap(a, b);
if (a%b == ) break;
if (a - b>b) break;
a -= b;
f = !f;
}
if (f)
puts("Stan wins");
else
puts("Ollie wins");
}
}