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. HDU44979 GCD and LCM (素因子分解+计数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意: 求有多少种(x,y,z)使得最小公倍数为l,最大公约数为g 分析: 我们将l,g进行素因 ...

  2. Invalid command &#39;WSGIScriptAlias&#39;, perhaps misspelled or defined by a module not included in the ser

    没有Include wsgi,执行: sudo a2enmod wsgi 可能出现以下的错误 ERROR: Module mod-wsgi does not exist! 安装 libapache2- ...

  3. Red Hat Linux 安装 (本地、网络安装)

    Red Hat Linux 安装 (本地.网络安装) 650) this.width=650;" onclick='window.open("http://blog.51cto.c ...

  4. customErrors 元素(ASP.NET 设置架构)

    1.适用版本:.NET Framework 4 2.元素定义:为 ASP.NET 应用程序提供有关自定义错误消息的信息. 可以在应用程序文件层次结构中的任意级别上定义 customErrors 元素. ...

  5. jquery基本Dom操作

    1 html()获取所有的html内容 2 html(value) 设置html内容,有html自动解析 3 text() 获取文本内容 4 text(value) 设置文本内容,有html自动转义 ...

  6. SQL语句查询数据库所有表和所有字段的详细信息(包括表描述和字段描述)

    select (case then ddd.value else '' end ) as "表名(中文)" --如果表名相同就返回空 , (case then d.name els ...

  7. ThinkPad X260 UEFI安装 win7 64位 方法

    ThinkPad X260   UEFI安装 win7 64位 方法 1.使用DG重新格式化硬盘,格式为GPT 2.使用CGI  安装 WIM文件 (image不知是否可以,下次测试) 3.改BIOS ...

  8. AOP 动态添加函数

    Function.prototype.before = function(beforefn) { // 保存原函数的引用 var self = this; // 返回包含了原函数和新函数的代理函数 r ...

  9. webstorm配置less解析的方法

    1.安装node.js 2.npm 安装less, npm install -g less 2.1 lessc style.less styles.css 编译 2.2 lessc –clean-cs ...

  10. BZOJ一句话

    一句话题解集合. 1061: [Noi2008]志愿者招募 单纯形,运用对偶原理转化过来,变成标准形然后单纯性裸上即可. #include<cmath> #include<cstdi ...