↓ 跳过正文
  1. Posts/

hdu 1850 Being a Good Boy in Spring Festival (nim游戏问必胜方案数,sg函数)

·422 字·1 分钟

hdu1850题目链接 题意:n堆石子。。每堆可以取任意多个。。。先取完的赢。。问先手能否赢。。能赢的话第一步有几种取法。。 思路:sg函数。。对于方案数,可以用nim游戏的结论。

以及。。sg函数。。如果走的步数是任意的。。也就是没有限制。。。那么sg[i] = i…此时也就退化成了一般的nim游戏。。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月20日 星期三 19时47分48秒
 4File Name :code/hdu/1850.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <map>
14#include <string>
15#include <cmath>
16#include <cstdlib>
17#include <ctime>
18#define fst first
19#define sec second
20#define lson l,m,rt<<1
21#define rson m+1,r,rt<<1|1
22#define ms(a,x) memset(a,x,sizeof(a))
23typedef long long LL;
24#define pi pair < int ,int >
25#define MP make_pair
26using namespace std;
27const double eps = 1E-8;
28const int dx4[4]={1,0,0,-1};
29const int dy4[4]={0,-1,1,0};
30const int inf = 0x3f3f3f3f;
31const int N=105;
32const int M=1E6+7;
33
34int n;
35int sg[M];
36int a[N];
37void sg_init()
38{
39    //这个可以记成结论23333
40    for ( int i = 1 ; i < M ; i ++) sg[i] = i ;
41}
42int main()
43{
44	#ifndef  ONLINE_JUDGE
45	freopen("code/in.txt","r",stdin);
46  #endif
47	sg_init();
48	while (~scanf("%d",&n))
49	{
50	    if (n==0) break;
51	    int sum = 0 ;
52	    for ( int i = 1 ; i <= n ; i++)
53	    {
54		int x;
55		scanf("%d",&x);
56		a[i] = x;
57		sum^=sg[x];
58	    }
59	    if (sum==0)
60	    {
61		puts("0");
62		continue;
63	    }
64	    int ans = 0 ;
65	    for ( int i = 1 ; i <= n ; i++)
66		if ((a[i]^sum)<a[i]) ans++;
67	    printf("%d\n",ans);
68	}
69  #ifndef ONLINE_JUDGE
70  fclose(stdin);
71  #endif
72    return 0;
73}

相关文章

hdu 1849Rabbit and Grass(一维nim游戏,sg函数)

·1290 字·3 分钟
Rabbit and Grass # **Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3058 Accepted Submission(s): 2261 ** Problem Description 大学时光是浪漫的,女生是浪漫的,圣诞更是浪漫的,但是Rabbit和Grass这两个大学女生在今年的圣诞节却表现得一点都不浪漫:不去逛商场,不去逛公园,不去和AC男约会,两个人竟然猫在寝食下棋…… 说是下棋,其实只是一个简单的小游戏而已,游戏的规则是这样的: 1、棋盘包含1*n个方格,方格从左到右分别编号为0,1,2,…,n-1; 2、m个棋子放在棋盘的方格上,方格可以为空,也可以放多于一个的棋子; 3、双方轮流走棋; 4、每一步可以选择任意一个棋子向左移动到任意的位置(可以多个棋子位于同一个方格),当然,任何棋子不能超出棋盘边界; 5、如果所有的棋子都位于最左边(即编号为0的位置),则游戏结束,并且规定最后走棋的一方为胜者。

hdu 1847 Good Luck in CET-4 Everybody! (巴什博奕,找规律||sg函数)

·635 字·2 分钟
hdu1847题目链接 题意:n个石子,每次只能取2的幂次个。。。问先手是否有必赢策略。。。 思路:画n点p点。。。发现n为3的倍数的时候先手必输。。。否则先手必赢。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年07月20日 星期三 18时54分46秒 4File Name :code/hdu/1847.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33int n; 34int main() 35{ 36 #ifndef ONLINE_JUDGE 37 freopen("code/in.txt","r",stdin); 38 #endif 39 40 while (scanf("%d",&n)!=EOF) 41 { 42 if (n%3==0) 43 { 44 puts("Cici"); 45 } 46 else 47 { 48 puts("Kiki"); 49 } 50 } 51 52 #ifndef ONLINE_JUDGE 53 fclose(stdin); 54 #endif 55 return 0; 56} 也可以利用sg函数求解。。。 从这里我们可以看出,sg函数类似于母函数,本身并不是一类问题,而是一种求解问题的工具。

nim游戏以及证明过程

·748 字·2 分钟
参考资料 (后面的证明写错了,差评,不要看,看图就好了) 1 1. 题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根, 可将一堆全取走,但不可不取,最后取完者为胜,求必胜的方法。

hdu 2147 kiki's game (巴什博奕)

·347 字·1 分钟
hdu 2147 题目链接 题意:一个n*m的方格,有一个棋子初始在右上角(1,m),每次可以将棋子向下或者向左或者向左下移动**一个格子,**不能移出边界,当无路可走的时候就输了,问谁存在必赢策略。