↓ 跳过正文
  1. Posts/

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函数类似于母函数,本身并不是一类问题,而是一种求解问题的工具。

代码实现
 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;
33const int N=1E3+7;
34int n;
35int two[12];
36int sg[N];
37bool vis[N];
38
39void sg_solve()
40{
41
42    two[0] = 1;
43    for ( int i = 1 ; i <=10 ; i++) two[i] = two[i-1]*2;
44    ms(sg,0);
45
46    for ( int i = 1 ; i < N ; i++)
47    {
48	ms(vis,false);
49	for ( int j = 0 ;  j <=10 ; j++)
50	    if (i-two[j]>=0) vis[sg[i-two[j]]] = true;
51	    //i-two[j]表示能从i到达的状态。。。 这里完全是按照定义在求。。。
52
53	for ( int j = 0 ; ; j++)
54	    if (!vis[j]) //第一个没有出现的值。。。
55	    {
56		sg[i] = j ;
57//		cout<<"j:"<<j<<endl;
58		break;
59	    }
60    }
61
62}
63int main()
64{
65	#ifndef  ONLINE_JUDGE
66	freopen("code/in.txt","r",stdin);
67  #endif
68
69	sg_solve();
70//	for ( int i = 1 ; i <= 100 ; i++) cout<<"i:"<<i<<" sg[i]"<<sg[i]<<endl;
71	while (scanf("%d",&n)!=EOF)
72	{
73	    if (!sg[n])
74	    {
75		puts("Cici");
76	    }
77	    else
78	    {
79		puts("Kiki");
80	    }
81	}
82
83  #ifndef ONLINE_JUDGE
84  fclose(stdin);
85  #endif
86    return 0;
87}

相关文章

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

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

hdu 1846 Brave Game (巴什博奕)

·634 字·2 分钟
hdu 1846 题目链接 题意:有n个石子,每次最多取m个,最少取1个,如果没有石子可取就输了。给出n,m,两个人都很聪明,问先手和后手谁赢。。 思路: 首先定义几个概念: p点:即必败点,某玩家位于此点,只要对方无失误,则必败

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 2149Public Sale(博弈论 巴什博奕)

·218 字·1 分钟
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 字·1 分钟
题目链接: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}