HDU 3980 Paint Chain (SG函数)

题意:给定一个圆环,上面有 n 个无色的珠子,然后有两个玩家,每次玩家要选连续的 m 个珠子染成不存在的颜色,染过的不能再染,谁不能染,谁输,问你谁胜。

析:一个SG函数的应用,首先这是一个环,不好做,把它变成线才好做,怎么变呢,很明显,第一个玩家涂是任意涂的,所以当它涂完,剩下的就可以成一个长度为 n - m 的线,不再是环了,只不过把第二个玩家当成选手再判断,然后现在考虑这是一条线,那么很明显每次都把该线分成了好几部分,而且这好几部分的游戏和,就是答案,也就是说这每一部分都是一个子游戏,只要把每一部分都异或的结果中选取没有存在的最小的自然数就是值。注意有几个特殊情况,第一个是,m > n 这个情况是后手胜 ,也就是一个都没染,第二个就是刚好能染一个人的,那么就是选手胜。

代码如下:

#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>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#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 fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#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 LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int maxm = 100 + 2;
const LL mod = 100000000;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -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 g[maxn], cnt[maxn];

int main(){
  int T;  cin >> T;
  for(int kase = 1; kase <= T; ++kase){
    scanf("%d %d", &n, &m);  n -= m;
    printf("Case #%d: ", kase);
    if(n < 0){ puts("abcdxyzk");  continue; }
    if(n < m){ puts("aekdycoin");  continue; }
    for(int i = 0; i < m; ++i)  g[i] = 0;
    for(int i = m; i <= n; ++i){
      for(int j = 0; j + m <= i; ++j)
        cnt[g[j]^g[i-j-m]] = i;
      for(int j = 0; ; ++j)
        if(cnt[j] != i){ g[i] = j;  break; }
    }
    puts(g[n] ? "abcdxyzk" : "aekdycoin");
  }
  return 0;
}

  

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