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. Oracle TIMESTAMP的处理

    public class Test { private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-M ...

  2. android动画-拖动

    先上图看效果 实质上说是动画有点不妥,确切的说应该是手势的处理,废话不多说看代码 SimpleDragSample.java public class SimpleDragSample extends ...

  3. CSU1608: Particle Collider(后缀数组)

    Description In the deep universe, there is a beautiful planet named as CS on which scientists have d ...

  4. iOS Code Sign error: Provisioning profile can&#39;t be found 解决方式

    出现error的过程:在执行另外一个xcode项目重置了code sign.回到原来的项目的时候出现这个error 修复方法: targe-build settings-code signing id ...

  5. 关于在天机项目中遇到的常用git 命令

    1. 本地分支和远程分支 1>我们在本地创建分支,第一次push到远程是没有分支存在,执行git push 会有提示,按照提示的内容操作即可,当然我们也可以 git push origin fe ...

  6. JAVA线程队列BlockingQueue

    JAVA线程队列BlockingQueue 介绍 BlockingQueue阻塞队列,顾名思义,首先它是一个队列,通过一个共享的队列,可以使得数据由队列的一端输入,从另外一端输出. 常用的队列主要有以 ...

  7. java带package的编译

    ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "java -cp d:\\TEST com/ ...

  8. ajax的几个面试题

    一.什么是AJAX(请谈一下你对Ajax的认识)AJAX是“Asynchronous JavaScript and XML”的缩写.他是指一种创建交互式网页应用的网页开发技术.Ajax包含下列技术:基 ...

  9. JS之预编译和执行顺序(全局和函数)

    预编译的两种情况 全局: 1.全局 直接是script标签中的代码,不包括函数执行 执行前: 1.首先生成一个GO(global object)对象,看不到,但是可以模拟出来用来分析 2.分析变量声明 ...

  10. OpenJDK源码研究笔记(十四):三种经典的设计方法,接口,接口-抽象类-具体实现类,接口-具体实现类

    在研究OpenJDK源码过程中,我发现常用的设计方法就是2种:接口,接口-抽象类-具体实现类 . 在一些其它开源框架和业务开发中,经常存在着第3种设计,接口-具体实现类. 1.只有接口,没有实现类. ...