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. javascript小白学习指南1---0

    第二章 变量和作用域    在看第二章时我希望,你能够回想一下前一次所讲的内容  假设有所遗忘 点这里    今天我们来说说 变量和作用域的问题 本章主要内容 基本类型和引用类型 运行环境 垃圾回收( ...

  2. BZOJ2895: 球队预算

    [传送门:BZOJ2895] 简要题意: 在一个篮球联赛里,有n支球队,球队的支出是和他们的胜负场次有关系的,具体来说,第i支球队的赛季总支出是Ci*x^2+Di*y^2,Di<=Ci.(赢得多 ...

  3. php数组增加元素

    php数组增加元素 截图 代码 <HTML> <HEAD> <TITLE>给数组增加元素</TITLE> </HEAD> <? $Ci ...

  4. 15:Challenge 11(主席树裸题)

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...

  5. Network Stack‎ : CookieMonster

    CookieMonster   The CookieMonster is the class in Chromium which handles in-browser storage, managem ...

  6. Ubuntu16.04+Gnome3 锁定屏幕快捷键无效解决办法

    Ubuntu16.04 桌面环境通过Ubuntu server和后安装的Gnome3 桌面环境实现,安装完以后发现锁定屏幕快捷键无效,系统设置=>键盘=>快捷中 锁屏快捷键已经存在Supe ...

  7. COWRUN

    USACO COWRUN 随机化搜索+双重递归调用 题面描述:给出8*N(<=14)组牌,每次按顺序选择8张,FJ可以选择前4张或者后4张,COW从FJ选出的牌中选择前两张或者后两张,然后COW ...

  8. jQuery 判断是否包含在数组中 jQuery.inArray()

    var arr = [ "mysql", "php", "css", "js" ];   $.inArray(" ...

  9. C++ lambda表达式 (一)

    为什么要lambda函数 匿名函数是许多编程语言都支持的概念,有函数体,没有函数名.1958年,lisp首先采用匿名函数,匿名函数最常用的是作为回调函数的值.正因为有这样的需求,c++引入了lambd ...

  10. 【Henu ACM Round#17 E】Tree Construction

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 做这题之前先要知道二叉排序树的一个性质. 就是它的中序遍历的结果就是这个数组升序排序. (且每个节点的左边的节点都是比这个节点的值小 ...