ACM 第十二天
博弈论(巴什博奕,威佐夫博弈,尼姆博弈,斐波那契博弈,SG函数,SG定理)
一. 巴什博奕(Bash Game):
A和B一块报数,每人每次报最少1个,最多报4个,看谁先报到30。这应该是最古老的关于巴什博奕的游戏了吧。
其实如果知道原理,这游戏一点运气成分都没有,只和先手后手有关,比如第一次报数,A报k个数,那么B报5-k个数,那么B报数之后问题就变为,A和B一块报数,看谁先报到25了,进而变为20,15,10,5,当到5的时候,不管A怎么报数,最后一个数肯定是B报的,可以看出,作为后手的B在个游戏中是不会输的。
那么如果我们要报n个数,每次最少报一个,最多报m个,我们可以找到这么一个整数k和r,使n=k*(m+1)+r,代入上面的例子我们就可以知道,如果r=0,那么先手必败;否则,先手必胜。
巴什博奕:只有一堆n个物品,两个人轮流从中取物,规定每次最少取一个,最多取m个,最后取光者为胜。
#include <iostream>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
if(n%(m+)==) cout<<"后手必胜"<<endl;
else cout<<"先手必胜"<<endl;
return ;
}
二. 威佐夫博弈(Wythoff Game):
有两堆各若干的物品,两人轮流从其中一堆取至少一件物品,至多不限,或从两堆中同时取相同件物品,规定最后取完者胜利。
直接说结论了,若两堆物品的初始值为(x,y),且x<y,则另z=y-x;
记w=(int)[((sqrt(5)+1)/2)*z ];
若w=x,则先手必败,否则先手必胜。
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int n1,n2,temp;
while(cin>>n1>>n2)
{
if(n1>n2) swap(n1,n2);
temp=floor((n2-n1)*(+sqrt(5.0))/2.0);
if(temp==n1) cout<<"后手必胜"<<endl;
else cout<<"先手必胜"<<endl;
}
return ;
}
三. 尼姆博弈(Nimm Game):
尼姆博弈指的是这样一个博弈游戏:有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品中取部分或全部物品,最少取一件,取到最后一件物品的人获胜。
结论就是:把每堆物品数全部异或起来,如果得到的值为0,那么先手必败,否则先手必胜。
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int n,ans,temp;
while(cin>>n)
{
temp=;
for(int i=;i<n;i++)
{
cin>>ans;
temp^=ans;
}
if(temp==) cout<<"后手必胜"<<endl;
else cout<<"先手必胜"<<endl;
}
return ;
}
四. 斐波那契博弈:
有一堆物品,两人轮流取物品,先手最少取一个,至多无上限,但不能把物品取完,之后每次取的物品数不能超过上一次取的物品数的二倍且至少为一件,取走最后一件物品的人获胜。
结论是:先手胜当且仅当n不是斐波那契数(n为物品总数)
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N = ;
int f[N];
void Init()
{
f[] = f[] = ;
for(int i=;i<N;i++)
f[i] = f[i-] + f[i-];
}
int main()
{
Init();
int n;
while(cin>>n)
{
if(n == ) break;
bool flag = ;
for(int i=;i<N;i++)
{
if(f[i] == n)
{
flag = ;
break;
}
}
if(flag) puts("Second win");
else puts("First win");
}
return ;
}
五、SG函数,SG定理
Sprague-Grundy定理(SG定理):
游戏和的SG函数等于各个游戏SG函数的Nim和。这样就可以将每一个子游戏分而治之,从而简化了问题。而Bouton定理就是Sprague-Grundy定理在Nim游戏中的直接应用,因为单堆的Nim游戏 SG函数满足 SG(x) = x。对博弈不是很清楚的请参照http://www.cnblogs.com/ECJTUACM-873284962/p/6398385.html进行进一步理解。
SG函数:
首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数。例如mex{0,1,2,4}=3、mex{2,3,5}=0、mex{}=0。
对于任意状态 x , 定义 SG(x) = mex(S),其中 S 是 x 后继状态的SG函数值的集合。如 x 有三个后继状态分别为 SG(a),SG(b),SG(c),那么SG(x) = mex{SG(a),SG(b),SG(c)}。 这样 集合S 的终态必然是空集,所以SG函数的终态为 SG(x) = 0,当且仅当 x 为必败点P时。
SG(x)=x
The first line contains an integer T(T≤100), indicates the number of test cases.
For each test case, the first line contains a single integer n(n≤1000), the number of lines of chessboard.
Then n lines, the first integer of ith line is m(m≤20), indicates the number of chesses on the ith line of the chessboard. Then m integers pj(1≤pj≤20) followed, the position of each chess.
OutputFor each test case, output one line of “YES” if Alice can win the game, “NO” otherwise.Sample Input
2
1
2 19 20
2
1 19
1 18
Sample Output
NO
YES
#include<bits/stdc++.h>
using namespace std;
int n,m,sg[<<],vis[];
int dfs(int x)
{
memset(vis,,sizeof(vis));
for(int i = ;i>=;i--)
{
if(x & (<<i))
{
int temp = x;
for(int j = i-;j>=;j--)
{
if(!(x&(<<j)))
{
temp ^= (<<j)^(<<i);
vis[sg[temp]]=;
break;
}
}
}
}
for(int i = ;i<=;i++)
if(!vis[i])
return i;
return ;
}
int main()
{
memset(sg,,sizeof(sg));
for(int i = ;i<(<<);i++)
sg[i]=dfs(i);
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int ans = ;
for(int i = ;i<n;i++)
{
int res = ,temp;
int q;
scanf("%d",&q);
while(q--)
scanf("%d",&temp),res|=<<(-temp);
ans^=sg[res];
}
if(ans)
printf("YES\n");
else
printf("NO\n");
} }
博弈练习题
今天,大家选择上机考试,就是一种勇敢(brave)的选择;这个短学期,我们讲的是博弈(game)专题;所以,大家现在玩的也是“勇敢者的游戏”,这也是我命名这个题目的原因。
当然,除了“勇敢”,我还希望看到“诚信”,无论考试成绩如何,希望看到的都是一个真实的结果,我也相信大家一定能做到的~
各位勇敢者要玩的第一个游戏是什么呢?很简单,它是这样定义的:
 
1、  本游戏是一个二人游戏;
 
2、  有一堆石子一共有n个;
 
3、  两人轮流进行;
 
4、  每走一步可以取走1…m个石子;
 
5、  最先取光石子的一方为胜;
如果游戏的双方使用的都是最优策略,请输出哪个人能赢。
 
 
每组测试数据占一行,包含两个整数n和m(1<=n,m<=1000),n和m的含义见题目描述。
Output如果先走的人能赢,请输出“first”,否则请输出“second”,每个实例的输出占一行。Sample Input
2
23 2
4 3
Sample Output
first
second
#include<stdio.h>
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
if(n%(m+)==)
printf("second\n");
else
printf("first\n");
}
return ;
}
Input
Output
Sample Input
2 1
8 4
4 7
Sample Output
0
1
0
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std; int main()
{
int n,m;
while(~scanf("%d%d",&m,&n))
{
int a=min(n,m);
int b=max(n,m);
double k=(double)(b-a);
int s1=(int)k*((+sqrt())/);
if(s1==a)
{
printf("0\n");
}
else
printf("1\n"); }
return ;
}
Output先取者负输出"Second win". 先取者胜输出"First win".
参看Sample Output.
Sample Input
2
13
10000
0
Sample Output
Second win
Second win
First win
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm> using namespace std; int main()
{
int n;
while(scanf("%d",&n)!=EOF && n!=)
{
int f[];
f[]=;
f[]=;
for(int i=; i<=; i++)
{
f[i]=f[i-]+f[i-];
}
int flag=;
for(int i=; i<=; i++)
{
if(n==f[i])
flag=;
}
if(flag==)
printf("Second win\n");
else printf("First win\n"); }
return ;
}
“升级”?“双扣”?“红五”?还是“斗地主”?
当然都不是!那多俗啊~
作为计算机学院的学生,Kiki和Cici打牌的时候可没忘记专业,她们打牌的规则是这样的:
1、 总共n张牌;
2、 双方轮流抓牌;
3、 每人每次抓牌的个数只能是2的幂次(即:1,2,4,8,16…)
4、 抓完牌,胜负结果也出来了:最后抓完牌的人为胜者;
假设Kiki和Cici都是足够聪明(其实不用假设,哪有不聪明的学生~),并且每次都是Kiki先抓牌,请问谁能赢呢?
当然,打牌无论谁赢都问题不大,重要的是马上到来的CET-4能有好的状态。
Good luck in CET-4 everybody!
 
 
Sample Input
1
3
Sample Output
Kiki
Cici
#include<stdio.h>
#include<math.h>
using namespace std; int main()
{
int n,m;
while(~scanf("%d",&n))
{
if(n%==) printf("Cici\n");
else
printf("Kiki\n");
}
return ;
}
The first line contains an integer T(T≤100), indicates the number of test cases.
For each test case, the first line contains a single integer n(n≤1000), the number of lines of chessboard.
Then n lines, the first integer of ith line is m(m≤20), indicates the number of chesses on the ith line of the chessboard. Then m integers pj(1≤pj≤20) followed, the position of each chess.
OutputFor each test case, output one line of “YES” if Alice can win the game, “NO” otherwise.Sample Input
2
1
2 19 20
2
1 19
1 18
Sample Output
NO
YES
#include<bits/stdc++.h>
using namespace std;
int n,m,sg[<<],vis[];
int dfs(int x)
{
memset(vis,,sizeof(vis));
for(int i = ;i>=;i--)
{
if(x & (<<i))
{
int temp = x;
for(int j = i-;j>=;j--)
{
if(!(x&(<<j)))
{
temp ^= (<<j)^(<<i);
vis[sg[temp]]=;
break;
}
}
}
}
for(int i = ;i<=;i++)
if(!vis[i])
return i;
return ;
}
int main()
{
memset(sg,,sizeof(sg));
for(int i = ;i<(<<);i++)
sg[i]=dfs(i);
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int ans = ;
for(int i = ;i<n;i++)
{
int res = ,temp;
int q;
scanf("%d",&q);
while(q--)
scanf("%d",&temp),res|=<<(-temp);
ans^=sg[res];
}
if(ans)
printf("YES\n");
else
printf("NO\n");
} }
integer n, m (0<n,m<=2000). The input is terminated when n=0 and
m=0.
OutputIf kiki wins the game printf "Wonderful!", else "What a pity!".
Sample Input
5 3
5 4
6 6
0 0
Sample Output
What a pity!
Wonderful!
Wonderful!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm> using namespace std; int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF &&( n!= || m!=))
{
if(n%== || m%==) printf("Wonderful!\n");
else
printf("What a pity!\n");
}
return ;
}
要种田得有田才行,Lele听说街上正在举行一场别开生面的拍卖会,拍卖的物品正好就是一块20亩的田地。于是,Lele带上他的全部积蓄,冲往拍卖会。
后来发现,整个拍卖会只有Lele和他的死对头Yueyue。
通过打听,Lele知道这场拍卖的规则是这样的:刚开始底价为0,两个人轮流开始加价,不过每次加价的幅度要在1~N之间,当价格大于或等于田地的成本价 M 时,主办方就把这块田地卖给这次叫价的人。
Lele和Yueyue虽然考试不行,但是对拍卖却十分精通,而且他们两个人都十分想得到这块田地。所以他们每次都是选对自己最有利的方式进行加价。
由于Lele字典序比Yueyue靠前,所以每次都是由Lele先开始加价,请问,第一次加价的时候,
 
Lele要出多少才能保证自己买得到这块地呢?
 
 
每组测试包含两个整数M和N(含义见题目描述,0<N,M<1100)
Output对于每组数据,在一行里按递增的顺序输出Lele第一次可以加的价。两个数据之间用空格隔开。
如果Lele在第一次无论如何出价都无法买到这块土地,就输出"none"。
Sample Input
4 2
3 2
3 5
Sample Output
1
none
3 4 5
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm> using namespace std;
int ans[];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n%(m+)==) printf("none\n");
else
{
if(n%(m+)==n)
{
printf("%d",n);
for(int i=n+;i<=m;i++)
{
printf(" %d",i);
}
}
else
{
printf("%d",n%(m+));
}
printf("\n");
} }
return ;
}
春节回家 你能做几天好孩子吗
寒假里尝试做做下面的事情吧
陪妈妈逛一次菜场
 
悄悄给爸爸买个小礼物
 
主动地 强烈地 要求洗一次碗
 
某一天早起 给爸妈用心地做回早餐
如果愿意 你还可以和爸妈说
 
咱们玩个小游戏吧 ACM课上学的呢~
下面是一个二人小游戏:桌子上有M堆扑克牌;每堆牌的数量分别为Ni(i=1…M);两人轮流进行;每走一步可以任意选择一堆并取走其中的任意张牌;桌子上的扑克全部取光,则游戏结束;最后一次取牌的人为胜者。
 
现在我们不想研究到底先手为胜还是为负,我只想问大家:
 
——“先手的人如果想赢,第一步有几种选择呢?”
 
 
Output如果先手的人能赢,请输出他第一步可行的方案数,否则请输出0,每个实例的输出占一行。
Sample Input
3
5 7 9
0
Sample Output
1
#include<cstring>
#include<cstdio> using namespace std; int main()
{
int t,a[]; while(scanf("%d",&t)!=EOF && t)
{
int sum=;
for(int i=;i<=t;i++)
{
scanf("%d",&a[i]);
sum^=a[i];
}
int ans=; if(sum==) printf("0\n");
else
{
int res;
for(int i=;i<=t;i++)
{
res=sum^a[i];
if(res<a[i]) ans++;
}
printf("%d\n",ans);
}
}
return ;
}
Now, they thought this game is too simple, and they want to change
some rules. In each turn one player must select a certain number of
consecutive unpainted beads to paint. The other rules is The same as the
 original. Who will win under the rules ?You may assume that both of
them are so clever.
line contain 2 integer N, M, indicate the chain has N beads, and each
turn one player must paint M consecutive beads. (1 <= N, M <=
1000)OutputFor each case, print "Case #idx: " first where idx is the case number start from 1, and the name of the winner.Sample Input
2
3 1
4 2
Sample Output
Case #1: aekdycoin
Case #2: abcdxyzk
#include<cstdio>
#include<cstring>
#include<cstdlib> #define MAXN 1010 using namespace std; int T,N,M,sg[MAXN];
int SG(int n)
{
if(sg[n]!=-) return sg[n];
if(n<M) return sg[n]=;
int vis[MAXN]= {};
//memset(vis,0,sizeof(vis));
for(int i=; i<=n-M-i; i++) vis[SG(i)^SG(n-M-i)]=;
for(int i=;; i++) if(!vis[i]) return sg[n]=i;
}
int main()
{ scanf("%d",&T);
int cas=;
while(T--)
{
scanf("%d%d",&N,&M);
printf("Case #%d: ",++cas);
memset(sg,-,sizeof(sg));
if(N<M)
{
printf("abcdxyzk\n");
continue;
}
if(SG(N-M)) printf("abcdxyzk\n");
else printf("aekdycoin\n");
}
return ;
}
F(1)=1;
F(2)=2;
F(n)=F(n-1)+F(n-2)(n>=3);
所以,1,2,3,5,8,13……就是菲波那契数列。
在HDOJ上有不少相关的题目,比如1005 Fibonacci again就是曾经的浙江省赛题。
今天,又一个关于Fibonacci的题目出现了,它是一个小游戏,定义如下:
1、 这是一个二人游戏;
2、 一共有3堆石子,数量分别是m, n, p个;
3、 两人轮流走;
4、 每走一步可以选择任意一堆石子,然后取走f个;
5、 f只能是菲波那契数列中的元素(即每次只能取1,2,3,5,8…等数量);
6、 最先取光所有石子的人为胜者;
假设双方都使用最优策略,请判断先手的人会赢还是后手的人会赢。
 
 
m=n=p=0则表示输入结束。
Output如果先手的人能赢,请输出“Fibo”,否则请输出“Nacci”,每个实例的输出占一行。
Sample Input
1 1 1
1 4 1
0 0 0
Sample Output
Fibo
Nacci
#include<cstdio>
#include<cstring>
#include<cstdlib> #define MAXN 1010 using namespace std;
int vis[];
int f[];
int sg[];
void get_sg()
{
f[]=;
f[]=;
for(int i=; i<=; i++)
{
f[i]=f[i-]+f[i-]; }
for(int i=; i<=; i++)
{
memset(vis,false,sizeof vis);
for(int j=; j<=; j++)
{
if(f[j]<=i) vis[sg[i-f[j]]]=true;
else break;
} for(int j=; j<=; j++)
{
if(!vis[j])
{
sg[i]=j;
break;
}
} }
} int main()
{
get_sg();
int m,n,p;
while(~scanf("%d%d%d",&m,&n,&p) &&!(m== && n== && p==))
{
int k=(sg[m]^sg[n]);
int t=(k^sg[p]);
if(t==) printf("Nacci\n");
else printf("Fibo\n");
}
return ;
}
ACM 第十二天的更多相关文章
- SCNU ACM 2016新生赛决赛 解题报告
		新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ... 
- SCNU ACM 2016新生赛初赛 解题报告
		新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ... 
- acm结束了
		最后一场比赛打完了.之前为了记录一些题目,开了这个博客,现在结束了acm,这个博客之后也不再更新了. 大家继续加油! 
- 关于ACM的总结
		看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ... 
- 第一届山东省ACM——Phone Number(java)
		Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ... 
- 第一届山东省ACM——Balloons(java)
		Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ... 
- ACM之鸡血篇
		一匹黑马的诞生 故事还要从南京现场赛讲起,话说这次现场赛,各路ACM英雄豪杰齐聚南京,为争取亚洲总舵南京分舵舵主之职位,都使出了看 家本领,其中有最有实力的有京城两大帮清华帮,北大帮,南郡三大派上交派 ... 
- 【codeforces 415D】Mashmokh and ACM(普通dp)
		[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ... 
- acm 1002 算法设计
		最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ... 
随机推荐
- 移动端利用canvas画布简单实现刮刮乐效果
			为了研究canvas一些属性简单实现的一个小效果 代码样式不太规范 随手写的 请问喷 初学者可以看下 css代码 <style> * { margin: 0; padding: 0; } ... 
- HBase学习(一):认识HBase
			一.大数据发展背景 现今是数据飞速膨胀的大数据时代,大数据强调3V特征,即Volume(量级).Varity(种类)和Velocity(速度). ·Volume(量级):TB到ZB. ·Varity( ... 
- 成都Uber优步司机奖励政策(3月15日)
			滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ... 
- UItraIso 制作ubentu 系统失败
			设备忙,请退出所有正在运行的应用程序,按确定按钮重试. 解决方法: 不要使用UItraIso,不知道为什么一直不行.重启了电脑几次都不行.用Rufus吧 https://rufus.ie/ 注意: r ... 
- 编译chromium时下载gn.exe时出错的解决方案
			天朝人写个代码真难,想要编译一下chromium,但是获取代码时各种坑,不是网速慢,就是网络联不通,真难玩. 本文针对下载gn.exe等工具时失败的解决方案. 原因1:gclient没有走代理,针对使 ... 
- 谁说接口不能有代码?—— Kotlin接口简介(KAD 26)
			作者:Antonio Leiva 时间:Jun 6, 2017 原文链接:https://antonioleiva.com/interfaces-kotlin/ 与Java相比,Kotlin接口允许你 ... 
- Git命令使用大全
			一前言 最近公司在使用vue和WebAPI前后端分离的项目开发,使用的代码管理工具是git,刚开始使用的时候前端的vue文件还比较好处理,但是后端的C#文件在每一次自己编译之后上传都会和其他小伙伴的代 ... 
- [JSON].valueOf( keyPath )
			语法:[JSON].valueOf( keyPath ) 返回:[任意类型 | null] 说明:获取键名路径原值,它保留原始值的类型 示例: b = sysFile.binary("tes ... 
- [JSON].remove( keyPath )
			语法:[JSON].remove( keyPath ) 返回:无 说明:移除指定路径的键 示例: Set jsonObj = toJson("{div:{'#text-1': 'is tex ... 
- lintcode 二分查找
			题目:二分查找 描述:给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. c ... 
