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;
    }
  • 相关阅读:
    SQL Server 复制订阅
    杂谈经验与未来
    泛泰A820L (高通MSM8660 cpu) 3.4内核的CM10.1(Android 4.2.2) 測试版第二版
    hdu1280 前m大的数(数组下标排序)
    Design Pattern Adaptor 适配器设计模式
    ssh命令、ping命令、traceroute 命令所使用的协议
    Android禁止ViewPager的左右滑动
    推荐一款优雅的jquery手风琴特效
    vijos
    iOS 7 UI 过渡指南
  • 原文地址:https://www.cnblogs.com/fenice/p/5737939.html
Copyright © 2011-2022 走看看