Nim or not Nim? HDU-3032 First use sg to hit the table, find the rule and then O(n) to solve it

Title

  • There are two options: ①Remove some stones from the pile ②Divide a pile of stones into two parts
  • Because of this question SI S_ISIIs in the range of 2 31 − 1 2^{31}-1231Within the range of 1 ,sg sgcannot be performedThe preprocessing of the s g function, so you can only choose to firstsg sgType a part of the table of the s g function, find out the rules, and then process each number.
  • According to the meaning of the title, the first step can be obtained for j ∈ [1, ai − 1] j\in[1,a_i-1]j[1,ai1],有 v i s [ s g [ j ] ] = t r u e vis[ sg[j] ]=true v i s [ s g [ j ] ]=true
  • According to the second step, a pile can be divided into two parts, assuming that the number of the first part is xxx,得出 v i s [ s g [ x ] ⊕ s g [ a i − x ] ] = t r u e vis[sg[x]\oplus sg[a_i-x]]=true V i s [ s G [ x ]s g [ aix]]=true
  • Finally, after playing out the rules of the table, we found that there are only three cases: when x% 4 = = 0 x\%4==0x%4==0 corresponds tosg sgs g value isx − 1 x-1x1 , whenx% 4 == 3 x\%4==3x%4==3 correspondingsg sgs g value isx + 1 x+1x+1 , the remaining cases arexxx
  • Finally, use this rule for each sg sgs g value proceed⊕ \oplus OK.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uii;
typedef pair<int, ll> pii;
template<typename T>
inline void rd(T& x)
{
    
    
	int tmp = 1; char c = getchar(); x = 0;
	while (c > '9' || c < '0') {
    
     if (c == '-')tmp = -1; c = getchar(); }
	while (c >= '0' && c <= '9') {
    
     x = x * 10 + c - '0'; c = getchar(); }
	x *= tmp;
}
const int N = 2e5 + 10;
const int M = 1e7 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int n, m, k;
int sg[N];
bool vis[N];
void SG(int x) {
    
    
	memset(vis, false, sizeof(vis));
	for (int i = 0; i < x; ++i) vis[sg[i]] = true;
	for (int j = 1; j <= x / 2; ++j) vis[sg[j] ^ sg[x - j]] = true;
	for (int i = 0;; ++i) {
    
    
		if (!vis[i]) {
    
    
			sg[x] = i;
			return;
		}
	}
}
int get(int x) {
    
    
	if (x % 4 == 0) return x - 1;
	else if (x % 4 == 3) return x + 1;
	return x;
}
int main() {
    
    
#ifdef _DEBUG
	//	FILE* _INPUT = freopen("input.txt", "r", stdin);
		//	FILE* _OUTPUT = freopen("output.txt", "w", stdout);
#endif // !_DEBUG
	int cas = 0, T = 0;
	sg[0] = 0;
	//打表如下
	//for (int i = 1; i <= 100; ++i) {
    
    
	//	SG(i);
	//	printf("%d :%d\n", i, sg[i]);
	//}
	rd(T);
	while (T--) {
    
    
		//	while (~scanf("%d", &n)) {
    
    
		int n; rd(n);
		for (int i = 1; i <= n; ++i) {
    
    
			int x; rd(x);
			sg[i] = get(x);
		}
		int ans = 0;
		for (int i = 1; i <= n; ++i) ans ^= sg[i];
		if (ans) puts("Alice");
		else puts("Bob");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/bloom_er/article/details/113774958