POJ 2348 Euclid's Game (博弈)

题意:给定两个整数,两个人轮流操作,每次可以用较大数减去较小数的整数倍,当一个数变成0时,则结束,问谁会胜。

析:很明显如果 a == b 那么就可以直接结束了,那么如果 a > b我们可以交换两个数,保证 a < b。可以分成两类,

(1) b - a < a (2)  b - a > a

对于第一类,只能一种拿法,只能是从 b 中拿去 a。对于第二种,如果 b 减去 a 后是必败态,那么当前就是必胜态,如果得到是必胜态,那么当前就是必败态,

我们假设 b - ax < a,如果 b - a(x-1)后是必败态,那么就可以直接减去,如果 b - a(x-1) 后是必胜态,那让b - ax 得到就是必败态。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e6 + 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

int main(){
  while(scanf("%d %d", &n, &m) == 2 && m+n){
    bool ok = true;
    while(1){
      if(m > n)  swap(m, n);
      if(n % m == 0)  break;
      if(n - m > m)  break;
      n -= m;
      ok = !ok;
    }
    printf("%s
", ok ? "Stan wins" : "Ollie wins");
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/6640464.html