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. C++学习笔记11-面向对象2

     1.  仅仅能初始化直接基类 一个类仅仅能初始化自己的直接基类.直接就是在派生列表中指定的类.假设类C 从类B 派生,类B 从类A 派生,则B 是C 的直接基类.尽管每一个C 类对象包括一个A 类部 ...

  2. Scala具体解释---------Scala是什么?可伸展的语言!

    Scala是什么 Scala语言的名称来自于"可伸展的语言". 之所以这样命名,是由于他被设计成随着使用者的需求而成长.你能够把Scala应用在非常大范围的编程任务上.从写个小脚本 ...

  3. [LeetCode]Single Number 异或的妙用

    1.数组中仅仅有一个元素仅仅出现一次,其余出现偶数次. 利用异或中同样元素相互抵消的方式求解. 2.数组中仅仅有2个元素仅仅出现一次.其余出现偶数次. class Solution { public: ...

  4. 小白学开发(iOS)OC_ 经常使用结构体(2015-08-14)

    // //  main.m //  经常使用结构体 // //  Created by admin on 15/8/13. //  Copyright (c) 2015年 admin. All rig ...

  5. session和cookie详解

    摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一 技术.本文将详细讨论session的工作机制并且对在Java ...

  6. HDU 5371 Hotaru's problem Manacher+尺取法

    题意:给你一个序列,求最长的两段回文子串,要求他们共用中间的一半. 思路:利用Manacher求出p[i]表示的当前位置的最长回文串长度,然后把每一个长度大于等于2的回文串的左区间和右区间分别放到两个 ...

  7. Linux下配置Squid基础教程

    Linux下配置Squid基础教程 本视频高清下载地址:http://down.51cto.com/data/437529 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处 ...

  8. epson 630打印机驱动安装不上

    1号机: 连接到630打印机的电脑 2号机: 通过网络连接到630打印机 *现状: 直接将数据线插在2号机上安装打印机时,驱动安装不上,设备管理器中有“!”号 *原因: 可能是已有一台通过网络连接到1 ...

  9. PHP获取随机字符串的两种方法

    <?php /** * 随机返回字符串 * @param number 返回字符串长度 * @param string 从哪些字符串中随机返回,已设置默认字符串,可空 * @return str ...

  10. 转:向IOS设备发送推送通知

    背景 SMS 和 MMS 消息是由无线运营商通过设备的电话号码向特定设备提供的.实现 SMS/MMS 的服务器端应用程序的开发人员必须费大量精力才能与现有的封闭电信基础架构进行交互(其中包括获取电话号 ...