HDU3595_GG and MM

/*
*State:  HDU3595    62MS    272K    1262 B    C++
*题目大意:
*        给定两个整数,两人轮流从较大的数中减去较小数的倍数(所有的结果数都非0)。
*        直到不能再减者为输。而且有n个游戏同时进行。
*解题思路:
*        先用gcd来求出谁胜谁负,当第一个出现a % b == 0 || a / b > 2的时候,这轮选
*        手就胜利,然后要记录路径,由最后的胜者来根据记录的路径来计算这一局的次数。
*        由于是n个游戏同时进行,而且可以走就要走光所有,所以只需记录最长的那一局
*        即是答案。
*解题感想:
*        gcd函数里面的vec.push_back()忘了加一个else,结果wa了3次,拖了半天。
*/
View Code
  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <vector>
  4 using namespace std;
  5 
  6 int cnt, first, flag, Max, res;
  7 vector<int> vec;
  8 
  9 int count()
 10 {
 11     int tol = 1, f = first;
 12 
 13     for(int i = vec.size() - 2; i >= 0; i--)
 14     {
 15         f = 1 - f;
 16         if(vec[i] == 1)
 17         {
 18             tol++;
 19         }
 20         else
 21         {
 22             if(f == first)
 23                 tol++;
 24             else
 25             {
 26                 tol += 2;
 27                 f = 1 - f;
 28             }
 29         }
 30     }
 31     return tol;
 32 }
 33 
 34 void gcd(int a, int b)
 35 {
 36     if(a % b == 0 || a / b > 1)
 37     {
 38         vec.push_back(a / b);
 39         if(!flag)
 40         {
 41             if(!(cnt & 1))
 42                 first = 1;
 43             flag = 1;
 44         }
 45         if(a % b == 0)
 46         {
 47             int tmp = count();
 48             if(Max < tmp)
 49             {
 50                 Max = tmp;
 51                 res = first;
 52             }
 53             return ;
 54         }
 55     }
 56     else
 57         vec.push_back(1);
 58     cnt++;
 59     gcd(b, a % b);
 60 }
 61 
 62 void init()
 63 {
 64     cnt = flag = 0;
 65     first = 0;
 66     vec.clear();
 67 }
 68 
 69 int main(void)
 70 {
 71 #ifndef ONLINE_JUDGE
 72     freopen("in.txt", "r", stdin);
 73 #endif
 74 
 75     int n;
 76     while(scanf("%d", &n) == 1)
 77     {
 78         Max = 0, res = 0;
 79         for(int i = 0; i < n; i++)
 80         {
 81             init();
 82             int a, b;
 83             scanf("%d %d", &a, &b);
 84             if(a < b)
 85             {
 86                 a = a ^ b;
 87                 b = a ^ b;
 88                 a = a ^ b;
 89             }
 90             if(!(a && b))
 91                 continue;
 92             gcd(a, b);
 93         }
 94         if(res)
 95             printf("MM\n");
 96         else
 97             printf("GG\n");
 98     }
 99     return 0;
100 }
原文地址:https://www.cnblogs.com/cchun/p/2613750.html