zoukankan      html  css  js  c++  java
  • HDU 5795 博弈

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5795

    A Simple Nim

    Time Limit: 2000/1000 MS (Java/Others)
    Memory Limit: 65536/65536 K (Java/Others)
    #### 问题描述 > Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes. #### 输入 > Intput contains multiple test cases. The first line is an integer 1≤T≤100, the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n−1], representing heaps with s[0],s[1],...,s[n−1] objects respectively.(1≤n≤106,1≤s[i]≤109) #### 输出 > For each test case,output a line whick contains either"First player wins."or"Second player wins". #### 样例 > **sample input** > 2 > 2 > 4 4 > 3 > 1 2 4 > > **sample output** > Second player wins. > First player wins.

    题解

    打表找规律,发现只有%8的位置的sg值与之前的一个数交换了,其它的sg值都等于它本身。

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    const int maxn = 111;
    
    int SG[maxn],vis[maxn+10];
    
    void get_SG() {
    	SG[0] = 0;
    	SG[1] = 1;
    	SG[2] = 2;
    	for (int i = 3; i < maxn; i++) {
    		memset(vis, 0, sizeof(vis));
    		for (int j = 0; j < i; j++) {
    			vis[SG[j]] = 1;
    		}
    		for (int a = 1; a < i; a++) {
    			for (int b = a; a + b < i; b++) {
    				int c = i - a - b;
    				vis[SG[a] ^ SG[b] ^ SG[c]] = 1;
    			}
    		}
    		for (int j = 0;; j++) if (vis[j] == 0) {
    			SG[i] = j; break;
    		}
    		/*printf("%d
    ", SG[i]);*/
    		if (SG[i] != i) printf("%d:%d
    ", i,SG[i]);
    	}
    }
    
    int main() {
    	//get_SG();
    	int tc,n;
    	scanf("%d", &tc);
    	while (tc--) {
    		scanf("%d", &n);
    		int ans = 0;
    		for (int i = 0; i < n; i++) {
    			int x; scanf("%d", &x);
    			if (x % 8==7) ans^=(x+1);
    			else if (x % 8 == 0) ans^=(x-1);
    			else ans ^= x;
    		}
    		if (!ans) puts("Second player wins.");
    		else puts("First player wins.");
    	}
    	return 0;
    }
  • 相关阅读:
    XUartPs_SetFifoThreshold
    函数指针 与 指针函数 的 区别
    zynq SPI 参数配置
    20199118《网络攻防实践》第三周作业
    maven项目将第三方jar包打入自己的jar中
    springboot项目简单的实现操作多数据库源
    WebSocket简单的应用
    怎么使用bat脚本更改本地受保护的hosts文件
    Spring Boot配置ssl证书启用HTTPS协议
    控制animation暂停:animation-play-state
  • 原文地址:https://www.cnblogs.com/fenice/p/5737939.html
Copyright © 2011-2022 走看看