Description

题库链接

两个人 Van♂ 游戏,每人手上各有 \(8\) 张牌,牌上数字均为 \([0,4]\) 之间的数。每个人在自己的回合选自己手牌中数字不为 \(0\) 的一张与对方手牌中不为 \(0\) 的一张。数字相加对 \(5\) 取模,赋给自己当前选中的这张牌。 \(T\) 组询问,给出先手,问谁能胜。

\(1\leq T\leq 100000\)

Solution

首先注意到卡牌顺序是没有影响的,我们可以按数字大小排序。这时由可重复的组合得每个人可能的方案只有 \(\binom{8+5-1}{8}=495\) 种。所以两个人不同的状态共 \(495^2=245025\) 种。

我们可以将每个状态抽象成节点,节点存下两个状态,改轮的操作者的状态和对方的状态。用有向边链接操作前的和操作后的情况。我们可以用 \(O\left(8^2\cdot\binom{8+5-1}{8}^2\right)\) 来枚举所有状态建边。对于每个节点我们考虑其连向的所有节点:

  1. 若存在一个连向的节点的状态能够保证当前的后手必败,那么改轮的操作者必胜;
  2. 若其连向的所有节点都是必胜,该节点必败;
  3. 其余的情况,则是平局。

我们可以通过反向建边 \(topsort\) 来实现。 \(O(1)\) 回答询问。

Code

//It is made by Awson on 2018.2.2
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 400000;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); } int S[N+5], C[N+5], cnt;
struct hs {
int a[8];
hs() {memset(a, 0, sizeof(a)); }
hs(int x) {for (int i = 7; i >= 0; i--) a[i] = x%5, x /= 5; }
hs(int *_a) {for (int i = 0; i < 8; i++) a[i] = _a[i]; }
void rd() {for (int i = 0; i < 8; i++) read(a[i]); }
void st() {sort(a, a+8); }
int hash() {
int ans = 0;
for (int i = 0; i < 8; i++) ans = ans*5+a[i];
return ans;
}
};
struct tt {int to, next; }edge[(N<<6)+5];
int path[N+5], top, in[N+5], ans[N+5]; void add(int u, int v) {++in[v]; edge[++top].to = v, edge[top].next = path[u]; path[u] = top; }
int no(int a, int b) {return (a-1)*cnt+b-1; }
void dfs(int cen, int last, int h) {
if (cen == 8) {S[++cnt] = h, C[h] = cnt; return; }
for (int i = last; i < 5; i++) dfs(cen+1, i, h*5+i);
}
void prework() {
dfs(0, 0, 0);
for (int i = 1; i <= cnt; i++)
for (int j = 1; j <= cnt; j++) {
hs a(S[i]), b(S[j]);
for (int p = 0; p < 8; p++) if (a.a[p])
for (int q = 0; q < 8; q++) if (b.a[q]) {
hs c(a.a);
c.a[p] = (a.a[p]+b.a[q])%5;
c.st(); int tmp = C[c.hash()];
add(no(j, tmp), no(i, j));
}
}
queue<int>Q; while (!Q.empty()) Q.pop();
for (int i = 2; i <= cnt; i++) ans[no(i, 1)] = 1, Q.push(no(i, 1));
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = path[u]; i; i = edge[i].next) if (!ans[edge[i].to]) {
if (ans[u] == 1) {ans[edge[i].to] = -1; Q.push(edge[i].to); }
else if (--in[edge[i].to] == 0) {
Q.push(edge[i].to); ans[edge[i].to] = 1;
}
}
}
}
void work() {
prework();
int t, f; hs a, b; read(t);
while (t--) {
read(f); a.rd(), b.rd(); a.st(); b.st();
if (f) swap(a, b);
int as = ans[no(C[a.hash()], C[b.hash()])];
if (as == 0) puts("Deal");
else if (as == -1 && f || as == 1 && !f) puts("Bob");
else puts("Alice");
}
}
int main() {
work();
return 0;
}

[Codeforces 919F]A Game With Numbers的更多相关文章

  1. Codeforces 919F. A Game With Numbers(博弈论)

      Imagine that Alice is playing a card game with her friend Bob. They both have exactly 88 cards and ...

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

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

  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. Educational Codeforces Round 2 A. Extract Numbers 模拟题

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

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

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

  8. codeforces 556B. Case of Fake Numbers 解题报告

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

  9. codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)

    In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...

随机推荐

  1. Java读取word中表格

    因为要新建一个站,公司要把word表格的部分行列存到数据库中.之前用java操作过excel,本来打算用java从word表格中读取数据,再存到数据库中,结果因为权限不够,无法访问公司要写的那个数据库 ...

  2. Build to win

    UPDATE:看到周筠老师的评论里的链接,那版式真的非常舒服.我想想模仿模仿他的布局来看看,虽然感觉做的也不是太好.另外对博客内容稍作修改. 一.获得小黄衫的感受 很幸运能够获得"领骑衫&q ...

  3. 双击CAD对象(具有扩展数据),显示自定义对话框实现方法

    转自:Cad人生 链接:http://www.cnblogs.com/cadlife/p/3463337.html 题目:双击CAD对象,显示自定义对话框实现方法 内容粘贴如下: 主要是绑定两个事件: ...

  4. java 零基础搭建dubbo运行环境

    一:简介    以前做项目时,分布式环境都是其它同事在搭建,自己也没参与分布式环境搭建,只负责开发,由于近段时间工作重心转到android,java后台有一段时间没有接触了,刚好这几天有空,决定自己动 ...

  5. Solaris 11 system package 安装与更新(如:assembler)

    最近在VirtualBox虚拟机中导入了Solaris 11.3.在里面安装Oracle数据库时,先行条件检查没通过,提示缺少程序包assembler. 在网上看了许多,这方面的信息还比较少.最后在O ...

  6. maven多环境部署

    1.首先在maven的pom.xml文件中添加profiles标签,然后分别添加3个不同环境的profile, 本例中添加了dev.test.product三个环境,这个可以根据自己的需要添加和减少. ...

  7. hadoop大数据技术架构详解

    大数据的时代已经来了,信息的爆炸式增长使得越来越多的行业面临这大量数据需要存储和分析的挑战.Hadoop作为一个开源的分布式并行处理平台,以其高拓展.高效率.高可靠等优点越来越受到欢迎.这同时也带动了 ...

  8. api-gateway实践(16)【租户模块:修改api定义】通过mq通知【开发者模块:更新开发者集市】

    一.订阅关系 二.接收消息 dev模块接收更新本地集市

  9. C# 文件操作类大全

      C# 文件操作类大全 时间:2015-01-31 16:04:20      阅读:1724      评论:0      收藏:0      [点我收藏+] 标签: 1.创建文件夹 //usin ...

  10. Django form表单

    Form介绍 之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来.与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否输入, ...