洛谷P4007 小 Y 和恐怖的奴隶主(期望dp 矩阵乘法)
题意
Sol
首先不难想到一种暴力dp,设\(f[i][a][b][c]\)表示还有\(i\)轮没打,场上有\(a\)个1血,\(b\)个2血,\(c\)个三血
发现状态数只有\(s = 166\)个,复杂度为\(O(ns)\)
矩乘优化一下复杂度为\(O(s^3 logn T)\),还是过不去。
因为每次询问都是独立的,那么可以预处理出\(2^i\)的转移矩阵,回答询问只需要拿一个行向量去乘log个矩阵
构造矩阵的时候可以加一个列向量表示期望
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int B = 60, mod = 998244353;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
LL mul(int x, int y) {return 1ll * x * y % mod;}
inline LL read() {
char c = getchar(); LL x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int fp(int a, int p) {
int base = 1;
while(p) {
if(p & 1) base = mul(base, a);
a = mul(a, a); p >>= 1;
}
return base;
}
int T, M, K;
namespace S3 {
int id[11][11][11], cnt, Lim;
int ans[168];
LL inv[11];
struct Ma {
int m[168][168];
Ma() {
memset(m, 0, sizeof(m));
}
void init() {
for(int i = 0; i <= Lim; i++) m[i][i] = 1;
}
void print() {
for(int i = 1; i <= Lim; i++, puts(""))
for(int j = 1; j <= Lim; j++)
printf("%d ", m[i][j]);
}
Ma operator * (const Ma &rhs) const {
Ma gg = {};
for(int i = 1; i <= Lim; i++)
for(int j = 1; j <= Lim; j++) {
__int128 tmp = 0;
for(int k = 1; k <= Lim; k++)
tmp += mul(m[i][k], rhs.m[k][j]);
tmp %= mod;
gg.m[i][j] = tmp;
}
return gg;
}
}f[B + 1];
void Pre() {
for(int i = 1; i <= K + 1; i++) inv[i] = fp(i, mod - 2);
for(int a = 0; a <= K; a++)
for(int b = 0; a + b <= K; b++)
for(int c = 0; a + b + c <= K; c++)
id[a][b][c] = ++cnt;
for(int a = 0; a <= K; a++)
for(int b = 0; a + b <= K; b++)
for(int c = 0; a + b + c <= K; c++) {
int down = inv[a + b + c + 1], tag = (a + b + c < K), now = id[a][b][c];
if(a) f[0].m[now][id[a - 1][b][c]] = mul(a, down);
if(b) f[0].m[now][id[a + 1][b - 1][c + tag]] = mul(b, down);
if(c) f[0].m[now][id[a][b + 1][c - 1 + tag]] = mul(c, down);
f[0].m[now][now] = down;
f[0].m[now][cnt + 1] = down;
}
f[0].m[cnt + 1][cnt + 1] = 1;
Lim = cnt + 1;
for(int i = 1; i <= B; i++) f[i] = f[i - 1] * f[i - 1];
}
int tmp[168];
void mul(Ma a) {
memset(tmp, 0, sizeof(tmp));
for(int j = 1; j <= Lim; j++)
for(int i = 1; i <= Lim; i++)
add2(tmp[j], 1ll * ans[i] * a.m[i][j] % mod);
memcpy(ans, tmp, sizeof(tmp));
}
void MatrixPow(LL p) {
for(int i = 0; p; p >>= 1, i++)
if(p & 1)
mul(f[i]);
}
void work() {
Pre();
while(T--) {
LL n = read();
memset(ans, 0, sizeof(ans)); ans[id[0][0][1]] = 1;
MatrixPow(n);
cout << ans[cnt + 1] << '\n';
}
}
}
namespace S2 {
int id[11][11], cnt, Lim;
int ans[168];
LL inv[11];
struct Ma {
int m[168][168];
Ma() {
memset(m, 0, sizeof(m));
}
void init() {
for(int i = 0; i <= Lim; i++) m[i][i] = 1;
}
void print() {
for(int i = 1; i <= Lim; i++, puts(""))
for(int j = 1; j <= Lim; j++)
printf("%d ", m[i][j]);
}
Ma operator * (const Ma &rhs) const {
Ma gg = {};
for(int i = 1; i <= Lim; i++)
for(int j = 1; j <= Lim; j++) {
__int128 tmp = 0;
for(int k = 1; k <= Lim; k++)
tmp += mul(m[i][k], rhs.m[k][j]);
tmp %= mod;
gg.m[i][j] = tmp;
}
return gg;
}
}f[B + 1];
void Pre() {
for(int i = 1; i <= K + 1; i++) inv[i] = fp(i, mod - 2);
for(int a = 0; a <= K; a++)
for(int b = 0; a + b <= K; b++)
id[a][b] = ++cnt;
for(int a = 0; a <= K; a++)
for(int b = 0; a + b <= K; b++) {
int down = inv[a + b + 1], tag = (a + b < K), now = id[a][b];
if(a) f[0].m[now][id[a - 1][b]] = mul(a, down);
if(b) f[0].m[now][id[a + 1][b - 1 + tag]] = mul(b, down);
f[0].m[now][now] = down;
f[0].m[now][cnt + 1] = down;
}
f[0].m[cnt + 1][cnt + 1] = 1;
Lim = cnt + 1;
for(int i = 1; i <= B; i++) f[i] = f[i - 1] * f[i - 1];
}
int tmp[168];
void mul(Ma a) {
memset(tmp, 0, sizeof(tmp));
for(int j = 1; j <= Lim; j++)
for(int i = 1; i <= Lim; i++)
add2(tmp[j], 1ll * ans[i] * a.m[i][j] % mod);
memcpy(ans, tmp, sizeof(tmp));
}
void MatrixPow(LL p) {
for(int i = 0; p; p >>= 1, i++)
if(p & 1)
mul(f[i]);
}
void work() {
Pre();
while(T--) {
LL n = read();
memset(ans, 0, sizeof(ans)); ans[id[0][1]] = 1;
MatrixPow(n);
cout << ans[cnt + 1] << '\n';
}
}
}
namespace S1 {
int N, f[12][9][9][9];
int inv(int a) {
return fp(a, mod - 2);
}
void work() {
N = 11;
for(int i = 1; i <= N; i++) {
for(int a = 0; a <= K; a++) {
for(int b = 0; a + b <= K; b++) {
for(int c = 0; a + b + c <= K; c++) {
int down = a + b + c + 1;
if(a) add2(f[i][a][b][c], mul(mul(a, inv(down)), f[i - 1][a - 1][b][c]));
if(b) {
if(down <= K) add2(f[i][a][b][c], mul(mul(b, inv(down)), f[i - 1][a + 1][b - 1 + (M == 2)][c + (M == 3)]));
else add2(f[i][a][b][c], mul(mul(b, inv(down)), f[i - 1][a + 1][b - 1][c]));
}
if(c) {
if(down <= K) add2(f[i][a][b][c], mul(mul(c, inv(down)), f[i - 1][a][b + 1 + (M == 2)][c - 1 + (M == 3)]));
else add2(f[i][a][b][c], mul(mul(c, inv(down)), f[i - 1][a][b + 1][c - 1]));
}
add2(f[i][a][b][c], mul(inv(down), f[i - 1][a][b][c] + 1));
}
}
}
}
while(T--) {
int n = read();
printf("%d\n", f[n][M == 1][M == 2][M == 3]);
}
}
}
int main() {
T = read(); M = read(); K = read();
if(M == 1) S1::work();
else if(M == 2) S2::work();
else S3::work();
return 0;
}
洛谷P4007 小 Y 和恐怖的奴隶主(期望dp 矩阵乘法)的更多相关文章
- 【loj2325】「清华集训 2017」小Y和恐怖的奴隶主 概率dp+倍增+矩阵乘法
题目描述 你有一个m点生命值的奴隶主,奴隶主受伤未死且当前随从数目不超过k则再召唤一个m点生命值的奴隶主. T次询问,每次询问如果如果对面下出一个n点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输 ...
- 【UOJ#340】【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划)
[UOJ#340][清华集训2017]小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划) 题面 UOJ 洛谷 题解 考虑如何暴力\(dp\). 设\(f[i][a][b][c]\)表示当前到了第\(i\) ...
- loj #2325. 「清华集训 2017」小Y和恐怖的奴隶主
#2325. 「清华集训 2017」小Y和恐怖的奴隶主 内存限制:256 MiB时间限制:2000 ms标准输入输出 题目类型:传统评测方式:文本比较 题目描述 "A fight? Co ...
- 洛谷P3830 随机树(SHOI2012)概率期望DP
题意:中文题,按照题目要求的二叉树生成方式,问(1)叶平均深度 (2)树平均深度 解法:这道题看完题之后完全没头绪,无奈看题解果然不是我能想到的qwq.题解参考https://blog.csdn.ne ...
- 洛谷P1291 [SHOI2002]百事世界杯之旅——期望DP
题目:https://www.luogu.org/problemnew/show/P1291 水水的经典期望DP: 输出有毒.(其实也很简单啦) 代码如下: #include<iostream& ...
- 洛谷P1373 小a和uim之大逃离 dp
正解:dp 解题报告: 传送门! 同样是看到列表发的题解就想着跟着做下dp的题目趴 然后发现还挺难的,,,反正我只大概想到怎么转移但是初始化什么的都不会TT 所以还是大概说下QAQ 首先可以想到设f[ ...
- [清华集训]小 Y 和恐怖的奴隶主
题面在这里 题意 有一个\(Boss\)和他血量为\(m\)的随从奴隶主,每当奴隶主受到攻击且不死,并且\(Boss\)的随从个数\(<k\)时,就会新召唤一个血量为\(m\)的奴隶主.每次攻击 ...
- uoj#340. 【清华集训2017】小 Y 和恐怖的奴隶主(矩阵加速)
传送门 uoj上的数据太毒了--也可能是我人傻常数大的缘故-- 三种血量的奴隶主加起来不超过\(8\)个,可以枚举每种血量的奴隶主个数,那么总的状态数只有\(165\)种,设\(dp_{t,i,j,k ...
- LibreOJ #2325. 「清华集训 2017」小Y和恐怖的奴隶主(矩阵快速幂优化DP)
哇这题剧毒,卡了好久常数才过T_T 设$f(i,s)$为到第$i$轮攻击,怪物状态为$s$时对boss的期望伤害,$sum$为状态$s$所表示的怪物个数,得到朴素的DP方程$f(i,s)=\sum \ ...
随机推荐
- C#实现录音录像录屏源码
以前写过两篇录音和录像的文章(实现语音视频录制.在服务器端录制语音视频),最近有朋友问,如果要实现屏幕录制这样的功能,该怎么做了?实际上录屏的原理跟录音.录像是差不多的,如果了解了我前面两篇文章中介绍 ...
- Presto + Superset 数据仓库及BI
基于Presto和superset搭建数据分析平台. Presto可以作为数据仓库,能够连接多种数据库和NoSql,同时查询性能很高: Superset提供了Presto连接,方便数据可视化和dash ...
- Request模块—数据解析工具
一.爬虫基本步骤 指定URL信息 发起请求 获取响应数据 对响应数据进行数据解析 持久化存储 二.数据解析 1. 正则表达式 (1) 基本语法 1. 单字符: . : 除换行以外所有字符 [] :[a ...
- php安装扩展的几种方法
转自:http://doc3.workerman.net/appendices/install-extension.html 安装扩展 注意 与Apache+PHP或者Nginx+PHP的运行模式不同 ...
- Linux - 利用systemctl命令管理服务
systemctl命令是系统服务管理器指令,融合了service和chkconfig的功能,可以查看和设置服务. 这里以docker服务为例. 利用systemctl命令管理 显示服务状态:syste ...
- OC学习3——C语言特性之指针
1.指针是C语言中的一个非常重要的概念,实际上,OC系统类的变量.自定义类的变量等都是指针.定义指针变量的语法格式如下,其中*代表一个指针变量,整个语法代表定义一个指向特定类型的变量的指针变量.注意: ...
- 《深入分析Linux内核源代码》读书、私藏笔记大放送
秉承着"不懂操作系统原理的程序员不是合格的程序员"的至理名言,鄙人又是买陈莉君老师的“Linux教学视频”,又是研读其力作<深入分析Linux内核源代码>,先将总结笔记 ...
- jq的ajax交互封装
jq封装的ajax,然后 在此前和此后都是很多要考虑的 ,何不 想想构思封装下. 下面: 基本上网页都存在各种ajax,使得网页变得更加易于操作. 举个长长的例子吧: <input type= ...
- Java 范例 - 字节处理
前言 Java 编程中常会遇到需要进行字节处理的地方,本篇文章就来探讨编程中会遇到的字节处理问题. 字节序 字节序(endianness)是对于多字节数据来说的,它描述了多字节数据存储的顺序,分为大端 ...
- 用Redis作Mysql数据库缓存
使用redis作mysql数据库缓存时,需要考虑两个问题: 1.确定用何种数据结构存储来自Mysql的数据; 2.在确定数据结构之后,用什么标识作为该数据结构的键. 直观上看,Mysql中的数据都是按 ...