[Codeforces 919F]A Game With Numbers
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)\) 来枚举所有状态建边。对于每个节点我们考虑其连向的所有节点:
- 若存在一个连向的节点的状态能够保证当前的后手必败,那么改轮的操作者必胜;
 - 若其连向的所有节点都是必胜,该节点必败;
 - 其余的情况,则是平局。
 
我们可以通过反向建边 \(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的更多相关文章
- 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 ...
 - 【题解】 Codeforces  919F A Game With Numbers(拓扑排序+博弈论+哈希)
		
懒得复制,戳我戳我 Solution: 我感觉我也说不太好,看Awson的题解吧. 说一点之前打错的地方: 连边存的是hash后的数组下标 if(ans[ num( C[a.hash()] , C[b ...
 - Codeforces 919F——A Game With Numbers
		
转自大佬博客:https://www.cnblogs.com/NaVi-Awson/p/8405966.html; 题意 两个人 Van♂ 游戏,每人手上各有 8'>88 张牌,牌上数字均为 [ ...
 - Codeforces 385C Bear and Prime Numbers
		
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
 - Codeforces 385C Bear and Prime Numbers(素数预处理)
		
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
 - Educational Codeforces Round 2 A. Extract Numbers 模拟题
		
A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
 - Educational Codeforces Round 8 D. Magic Numbers 数位DP
		
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
 - codeforces  556B. Case of Fake Numbers  解题报告
		
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
 - codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
		
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
 
随机推荐
- alpha冲刺第四天
			
一.合照 二.项目燃尽图 三.项目进展 今天实现了登录界面和服务器的连接了,牵手成功. 一些具体的界面细化实现,一些button的响应实现 四.明日规划 登录界面和服务器的连接实现耗费了太多时间,接下 ...
 - 高级软件工程第三次作业 赵坤&黄亦薇
			
0.小组成员 赵坤2017282110261 黄亦薇201728210260 1.项目Github地址 https://github.com/zkself/homework3 PS:建议使用chro ...
 - docopt——好用的Python命令行参数解释器
			
Qingchat使用的命令行参数解释器是 docopt,用下来感觉非常棒,所以决定介绍一下这个库.( 奉劝各位看官,真爱生命,远离argparse. ) 介绍 docopt 本质上是在 Python ...
 - 几种Java的JSON解析库速度对比
			
java中哪个JSON库的解析速度是最快的? JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考 了.我们很少会去 ...
 - cord-in-a-box 2.0 安装指南
			
[TOC] 这篇文章简要介绍了 Ciab2.0 的安装. 包括硬件, 软件环境的选择, Ciab2.0的实际部署架构, 安装过程等. 下面就先对 Ciab2.0 部署环境做简要介绍. 1. 概述 这一 ...
 - 识别图片中文字(百度AI)
			
这个是百度官方的文档 https://ai.baidu.com/docs#/OCR-API/top 通用的文字识别,如果是其他的含生僻字/含位置信息的版本,请参考官方的文档,只 ...
 - 关于APIcloud对应C#的 wcf框架作为后台,实现多库功能
			
首先,我是使用ajax原来的请求方式,并没有使用apicloud中封装的请求方式. 前端代码: function makeRequest() { //alert("inside makeRe ...
 - Andrew Ng机器学习第一章——初识机器学习
			
机器学习的定义 计算机程序从经验E中学习,解决某一任务T.进行某一性能度量P,通过P测定在T上的表现因E而提高. 简而言之:程序通过多次执行之后获得学习经验,利用这些经验可以使得程序的输出结果更为理想 ...
 - 新概念英语(1-141)Sally's first train ride
			
Lesson 141 Sally's first train ride 萨莉第一交乘火车旅行 Listen to the tape then answer this question. Why was ...
 - angular2 学习笔记 ( angular cli & npm version manage npm 版本管理 )
			
更新 : 2017-05-05 现在流行 Yarn ! 它是 facebook google 推出的东西. 算是补助 npm 做的不够好的地方. 源码依然是发布去 npm,只是下载接口换掉罢了哦. n ...