↓ Skip to main content
  1. Posts/

hdu 1846 Brave Game (巴什博奕)

·634 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

hdu 1846 题目链接

题意:有n个石子,每次最多取m个,最少取1个,如果没有石子可取就输了。给出n,m,两个人都很聪明,问先手和后手谁赢。。

思路:

首先定义几个概念:

p点:即必败点,某玩家位于此点,只要对方无失误,则必败

**  N点 :即必胜点,某玩家位于此点,只要自己无失误,则必胜。**

** 一、 所有终结点都是必败点P(这道题目中,轮到谁取石子,还剩0个石子的时候,此人无石子可取,就输了);**

** 二、所有一步能走到必败点P的就是N点;(这里是_存在一种_情况可以走到p点即可)**

** 三、通过一步操作只能到N点的就是P点; (这里是_所有_的情况都只能走到n点)**

那么当m=3的时候,则有:

x :0 1 2 3 4 5 6 7 8 9 10…

pos:P N N N P N N N P N N …

1,2,3为n点是因为一步可以走到p点0,直观得说就是剩余1,2,3个石子的时候可以一次拿走。

4为p点是因为,不管怎么走,下一步一定是处于1,2,3这三个n点的,因此4是p点。

因此我们可以得出结论:n%(m+1)==0的时候,后手赢,否则先手赢。

(之前遇到的时候只记了结论,不清楚为什么,这下明白了orz

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月19日 星期二 19时41分16秒
 4File Name :code/hdu/1846.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,m;
34int main()
35{
36	#ifndef  ONLINE_JUDGE
37	freopen("code/in.txt","r",stdin);
38  #endif
39	int T;
40	cin>>T;
41	while (T--)
42	{
43	    scanf("%d%d",&n,&m);
44	    if (n%(m+1)==0)
45	    {
46		puts("second");
47	    }
48	    else
49	    {
50		puts("first");
51	    }
52	}
53
54  #ifndef ONLINE_JUDGE
55  fclose(stdin);
56  #endif
57    return 0;
58}

Related

hdu 2149Public Sale(博弈论 巴什博奕)

·218 words·1 min
hdu 2149题目链接 题意&思路:巴什博奕,点m是n点。。。然后往前画即可。。。 代码实现 1/************************************************************************* 2 > File Name: code/hdu/2149.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月22日 星期二 20时18分02秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#include<cctype> 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define ms(a,x) memset(a,x,sizeof(a)) 25#define lr dying111qqz 26using namespace std; 27#define For(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef double DB; 30const int inf = 0x3f3f3f3f; 31int n,m; 32int main() 33{ 34 #ifndef ONLINE_JUDGE 35 freopen("in.txt","r",stdin); 36 #endif 37 while (scanf("%d %d",&m,&n)!=EOF) 38 { 39 if (m<n+1) 40 { 41 printf("%d",m); 42 for ( int i = m+1 ; i <= n; i++) 43 printf(" %d",i); 44 printf("\n"); 45 continue; 46 } 47 int tmp = m%(n+1); 48 if (tmp>0) 49 { 50 printf("%d\n",tmp); 51 } 52 else 53 { 54 puts("none"); 55 } 56 57 } 58 59 #ifndef ONLINE_JUDGE 60 fclose(stdin); 61 #endif 62 return 0; 63}

hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者 (巴什博奕)

·195 words·1 min
题目链接:hdu 2188题目链接 题意&思路:巴什博奕。。画n点p点。。。 代码实现 1/************************************************************************* 2 > File Name: code/hdu/2188.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月22日 星期二 20时08分08秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#include<cctype> 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define ms(a,x) memset(a,x,sizeof(a)) 25#define lr dying111qqz 26using namespace std; 27#define For(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef double DB; 30const int inf = 0x3f3f3f3f; 31void solve() 32{ 33 int n,m; 34 scanf("%d %d",&n,&m); 35 if (n%(m+1)!=0) 36 { 37 puts("Grass"); 38 } 39 else 40 { 41 puts("Rabbit"); 42 } 43} 44int main() 45{ 46 #ifndef ONLINE_JUDGE 47 freopen("in.txt","r",stdin); 48 #endif 49 int T; 50 cin>>T; 51 while (T--) 52 { 53 solve(); 54 } 55 #ifndef ONLINE_JUDGE 56 fclose(stdin); 57 #endif 58 return 0; 59}

codeforces #334 div 2 E. Lieges of Legendre

·387 words·1 min
http://codeforces.com/contest/604/problem/E 题意:有两个人做游戏,游戏规则如下: 有n堆石子,每次可以对一堆石子进行操作,如果当前石子是偶数,那么可以选择将这2*x个石子分成k堆石子数为x的石子堆,还有一种没有前提的操作是取走当前堆的一个石子,问先手赢还是后手赢,先手和后手都足够聪明的情况下。 思路:博弈论。。不会做。。第一次接触sg函数。。转载一篇题解: http://m.blog.csdn.net/blog/qq_24451605/50154973

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

·1290 words·3 mins
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的位置),则游戏结束,并且规定最后走棋的一方为胜者。