Imagine that Alice is playing a card game with her friend Bob. They both have exactly 88 cards and there is an integer on each card, ranging from 00 to 44. In each round, Alice or Bob in turns choose two cards from different players, let them be aa and bb, where aa is the number on the player's card, and bb is the number on the opponent's card. It is necessary that a⋅b≠0a⋅b≠0. Then they calculate c=(a+b)mod5c=(a+b)mod5 and replace the number aa with cc. The player who ends up with numbers on all 88 cards being 00, wins.

Now Alice wants to know who wins in some situations. She will give you her cards' numbers, Bob's cards' numbers and the person playing the first round. Your task is to determine who wins if both of them choose the best operation in their rounds.

Input

The first line contains one positive integer TT (1≤T≤1000001≤T≤100000), denoting the number of situations you need to consider.

The following lines describe those TT situations. For each situation:

  • The first line contains a non-negative integer ff (0≤f≤10≤f≤1), where f=0f=0 means that Alice plays first and f=1f=1 means Bob plays first.
  • The second line contains 88 non-negative integers a1,a2,…,a8a1,a2,…,a8 (0≤ai≤40≤ai≤4), describing Alice's cards.
  • The third line contains 88 non-negative integers b1,b2,…,b8b1,b2,…,b8 (0≤bi≤40≤bi≤4), describing Bob's cards.

We guarantee that if f=0f=0, we have ∑8i=1ai≠0∑i=18ai≠0. Also when f=1f=1, ∑8i=1bi≠0∑i=18bi≠0 holds.

Output

Output TT lines. For each situation, determine who wins. Output

  • "Alice" (without quotes) if Alice wins.
  • "Bob" (without quotes) if Bob wins.
  • "Deal" (without quotes) if it gets into a deal, i.e. no one wins.

解题思路:

博弈论,假如说做出一个决定,之后做出的可能的决定存在先手必败,那么这个先手一定像那个状态选择,这样后手作为新的新手就一定必败。

而如果后继状态中只要有先手必胜,那么这个人一定尽量不选择这个状态。

将状态抽象成点,将可以转移到的状态之间连上有向边,就出现了一个图。

比如说这道题,可以将可能的状态(4952个)连边,当然我们要反向处理。

确定先手必胜时BFS,否则拓扑排序。

细节要好好处理,二人是不会使用0去更新的(见题目描述)。

代码:

 #include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
struct pnt{
std::pair<int,int>sit;
int hd;
int ind;
int fin;//-1 先手必败
}p[];
struct ent{
int twd;
int lst;
}e[];
std::queue<int>Q;
std::map<int,int>M1;
std::map<std::pair<int,int>,int>M2;
int S[][];
int H[];
int tmp[];
int cnt;
int n,m;
int T;
int trans(int *a);
int indx(int sd);
void dfs(int x);
void zip(void);
void build(void);
void Bfs(void);
void markimp(void);
void addedge(void);
void work(void);
void Pre(void);
void ade(int f,int t);
int main()
{
Pre();
scanf("%d",&T);
while(T--)
work();
return ;
}
void Pre(void)
{
dfs();
zip();
build();
Bfs();
return ;
}
void dfs(int x)
{
if(x==)
{
m++;
for(int i=;i<=;i++)
{
S[m][i]=tmp[i];
H[m]=H[m]*+tmp[i];
}
M1[H[m]]=m;
return ;
}
for(int i=tmp[x-];i<=;i++)
{
tmp[x]=i;
dfs(x+);
}
return ;
}
void zip(void)
{
for(int i=;i<=m;i++)
{
for(int j=;j<=m;j++)
{
p[++n].sit=std::make_pair(i,j);
M2[std::make_pair(i,j)]=n;
}
}
return ;
}
void build(void)
{
markimp();
addedge();
return ;
}
void Bfs(void)
{
while(!Q.empty())
{
int x=Q.front();
Q.pop();
for(int i=p[x].hd;i;i=e[i].lst)
{
int to=e[i].twd;
if(p[to].ind==)
continue;
if(p[x].fin==-)
{
p[to].ind=;
p[to].fin=;
Q.push(to);
}else{
p[to].ind--;
if(!p[to].ind&&!p[to].fin)
{
p[to].fin=-;
Q.push(to);
}
}
}
}
return ;
}
void markimp(void)
{
int sta=M1[];
for(int i=;i<=n;i++)
{
if(p[i].sit.first==sta)
{
p[i].fin=;
Q.push(i);
}else if(p[i].sit.second==sta)
{
p[i].fin=-;
Q.push(i);
}
}
return ;
}
void addedge(void)
{
for(int x=;x<=n;x++)
{
if(p[x].fin)
continue;
for(int i=;i<=;i++)
tmp[i]=S[p[x].sit.first][i];
for(int f=;f<=;f++)
{
if(f!=&&tmp[f]==tmp[f-])
continue;
if(!tmp[f])
continue;
int a=tmp[f];
for(int t=;t<=;t++)
{
if(t!=&&S[p[x].sit.second][t]==S[p[x].sit.second][t-])
continue;
int b=S[p[x].sit.second][t];
if(!b)
continue;
int c=(a+b)%;
tmp[f]=c;
int t0=M1[trans(tmp)];
int y=M2[std::make_pair(p[x].sit.second,t0)];
ade(y,x);
for(int i=;i<=;i++)
tmp[i]=S[p[x].sit.first][i];
}
}
}
}
int trans(int *a)
{
int ans=;
std::sort(a+,a+);
for(int i=;i<=;i++)
ans=ans*+a[i];
return ans;
}
void ade(int f,int t)
{
cnt++;
e[cnt].twd=t;
e[cnt].lst=p[f].hd;
p[f].hd=cnt;
p[t].ind++;
return ;
}
void work(void)
{
int f;
scanf("%d",&f);
int t0,t1;
for(int i=;i<=;i++)
scanf("%d",&tmp[i]);
t0=M1[trans(tmp)];
for(int i=;i<=;i++)
scanf("%d",&tmp[i]);
t1=M1[trans(tmp)];
if(f)
std::swap(t0,t1);
if(f)
{
int x=M2[std::make_pair(t0,t1)];
if(p[x].fin==)
{
puts("Bob");
}else if(p[x].fin==)
{
puts("Deal");
}else{
puts("Alice");
}
}else{
int x=M2[std::make_pair(t0,t1)];
if(p[x].fin==-)
{
puts("Bob");
}else if(p[x].fin==)
{
puts("Deal");
}else{
puts("Alice");
}
}
}

Codeforces 919F. A Game With Numbers(博弈论)的更多相关文章

  1. 【题解】 Codeforces 919F A Game With Numbers(拓扑排序+博弈论+哈希)

    懒得复制,戳我戳我 Solution: 我感觉我也说不太好,看Awson的题解吧. 说一点之前打错的地方: 连边存的是hash后的数组下标 if(ans[ num( C[a.hash()] , C[b ...

  2. [Codeforces 919F]A Game With Numbers

    Description 题库链接 两个人 Van♂ 游戏,每人手上各有 \(8\) 张牌,牌上数字均为 \([0,4]\) 之间的数.每个人在自己的回合选自己手牌中数字不为 \(0\) 的一张与对方手 ...

  3. Codeforces 919F——A Game With Numbers

    转自大佬博客:https://www.cnblogs.com/NaVi-Awson/p/8405966.html; 题意 两个人 Van♂ 游戏,每人手上各有 8'>88 张牌,牌上数字均为 [ ...

  4. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  5. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  6. Codeforces Round #114 (Div. 1) C. Wizards and Numbers 博弈论

    C. Wizards and Numbers 题目连接: http://codeforces.com/problemset/problem/167/C Description In some coun ...

  7. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  8. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  9. codeforces 1451D,一道有趣的博弈论问题

    大家好,欢迎来到codeforces专题. 今天选择的问题是Contest 1451场的D题,这是一道有趣简单的伪博弈论问题,全场通过的人有3203人.难度不太高,依旧以思维为主,坑不多,非常友好. ...

随机推荐

  1. Yocto tips (10): Yocto hellworld 加入一个软件包

    Yocto中一个软件包是放在bb文件里的,然后非常多的bb文件集成一个recipe(配方),然后很多的recipe又组成一个meta layer.因此,要加入一个包事实上就是在recipe以下加入一个 ...

  2. CF Mike and Feet (求连续区间内长度为i的最小值)单调栈

    Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. 5.应用与模块(ng-app)

    转自:https://www.cnblogs.com/best/tag/Angular/ 自动载入启动一个AngularJS应用,声明了ng-app的元素会成为$rootScope的起点 每个HTML ...

  4. C# 从磁盘中读取文件

    读取txt文件 ------读取的数据比较小的时候: 如果你要读取的文件内容不是很多,可以使用 File.ReadAllText(filePath) 或指定编码方式 File.ReadAllText( ...

  5. Windows环境下VMware虚拟机的自启动与自动关机--命令行操作

    .设置开机免密登录系统 1. 按下Windows + R 组合键,输入“netplwiz”,点击回车. 2. 去除需要密码登录的勾. 3. 如果需要密码,输入密码,点击确认.   二.编辑vmware ...

  6. JS之闭包详细解读

    闭包在红宝书中的解释就是:有权访问另一个函数作用域中的变量的函数. 1.变量作用域 全局变量:所有的函数外部定义的变量,它的作用域是整个script. 局部变量:定义在函数体内部的变量,作用域仅限于函 ...

  7. Mybatis mapper.xml文件头文件备份

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  8. 洛谷 P2117 小Z的矩阵

    P2117 小Z的矩阵 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1,则G(A)等于所有A[i][j]*A[j][i]的和对2取余 ...

  9. MySQL架构组成之逻辑模块组成

    MySQL 能够看成是二层架构   第一层SQL Layer.包含权限推断.sql 解析.运行计划优化,query cache 的处理等等.   第二层存储引擎层(Storage Engine Lay ...

  10. IOS学习之斯坦福大学IOS开发课程笔记(第六课)

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28398697 作者:小马 这节课主要讲述多个MVC是怎样协同工作的.到眼下为止.全 ...