题意

题目链接

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 矩阵乘法)的更多相关文章

  1. 【loj2325】「清华集训 2017」小Y和恐怖的奴隶主 概率dp+倍增+矩阵乘法

    题目描述 你有一个m点生命值的奴隶主,奴隶主受伤未死且当前随从数目不超过k则再召唤一个m点生命值的奴隶主. T次询问,每次询问如果如果对面下出一个n点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输 ...

  2. 【UOJ#340】【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划)

    [UOJ#340][清华集训2017]小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划) 题面 UOJ 洛谷 题解 考虑如何暴力\(dp\). 设\(f[i][a][b][c]\)表示当前到了第\(i\) ...

  3. loj #2325. 「清华集训 2017」小Y和恐怖的奴隶主

    #2325. 「清华集训 2017」小Y和恐怖的奴隶主 内存限制:256 MiB时间限制:2000 ms标准输入输出 题目类型:传统评测方式:文本比较   题目描述 "A fight? Co ...

  4. 洛谷P3830 随机树(SHOI2012)概率期望DP

    题意:中文题,按照题目要求的二叉树生成方式,问(1)叶平均深度 (2)树平均深度 解法:这道题看完题之后完全没头绪,无奈看题解果然不是我能想到的qwq.题解参考https://blog.csdn.ne ...

  5. 洛谷P1291 [SHOI2002]百事世界杯之旅——期望DP

    题目:https://www.luogu.org/problemnew/show/P1291 水水的经典期望DP: 输出有毒.(其实也很简单啦) 代码如下: #include<iostream& ...

  6. 洛谷P1373 小a和uim之大逃离 dp

    正解:dp 解题报告: 传送门! 同样是看到列表发的题解就想着跟着做下dp的题目趴 然后发现还挺难的,,,反正我只大概想到怎么转移但是初始化什么的都不会TT 所以还是大概说下QAQ 首先可以想到设f[ ...

  7. [清华集训]小 Y 和恐怖的奴隶主

    题面在这里 题意 有一个\(Boss\)和他血量为\(m\)的随从奴隶主,每当奴隶主受到攻击且不死,并且\(Boss\)的随从个数\(<k\)时,就会新召唤一个血量为\(m\)的奴隶主.每次攻击 ...

  8. uoj#340. 【清华集训2017】小 Y 和恐怖的奴隶主(矩阵加速)

    传送门 uoj上的数据太毒了--也可能是我人傻常数大的缘故-- 三种血量的奴隶主加起来不超过\(8\)个,可以枚举每种血量的奴隶主个数,那么总的状态数只有\(165\)种,设\(dp_{t,i,j,k ...

  9. LibreOJ #2325. 「清华集训 2017」小Y和恐怖的奴隶主(矩阵快速幂优化DP)

    哇这题剧毒,卡了好久常数才过T_T 设$f(i,s)$为到第$i$轮攻击,怪物状态为$s$时对boss的期望伤害,$sum$为状态$s$所表示的怪物个数,得到朴素的DP方程$f(i,s)=\sum \ ...

随机推荐

  1. MySQL 基础--字符类型

    ##=====================================================================================## MySQL支持的字符 ...

  2. 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调

    [源码下载] 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调 作者: ...

  3. 【接口时序】1、软件与Verilog基本格式规范说明

    一. 说明 以前总是没有记录的习惯,导致遇到问题时总得重新回忆与摸索,大大降低了学习效率,从今天开始决定改掉这个坏毛病,认真记录自己的Verilog学习之路,希望自己能一直坚持下去. 二. 软件资源与 ...

  4. fatal: protocol error: bad line length character: This

    昨晚尝试搭建一个Git服务器,在搭建好服务器后,在服务器创建了一个空项目,我在本地使用git clone 拉取项目时,报了fatal: protocol error: bad line length ...

  5. [CocoaPods]Podfile文件

    Podfile是一个描述一个或多个Xcode项目的目标依赖项的规范.该文件应该只是命名Podfile.指南中的所有示例都基于CocoaPods 1.0及更高版本. Podfile可以非常简单,这会将A ...

  6. 使用cygwin中的awk工具进行mysql binlog日志查看[利刃篇]

    linux工具确实强悍,然而作为没有linux机器使用权以及开发没有使用linux进行的人,有时想用一些命令确实不方便,所以,才去试着用用cygwin,一款在windows平台上运行的类UNIX模拟环 ...

  7. 分布式任务调度系统xxl-job搭建

    为解决分布式环境下定时任务的可靠性,稳定性,只执行一次的特性,我找到了个大众点评开源的分布式调度任务解决完整系统,下面我将一步步深入解读该系统,从基本的使用到源码的探究 下载 https://gith ...

  8. Xamarin.Android 使用 SQLite 出现 Index -1 requested, with a size of 10 异常

    异常: Android.Database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 10 此错误是数据返回 ...

  9. odoo开发笔记 -- 日常开发注意点总结(持续补充)

    (1) odoo视图字段,如果是readonly,默认该数据是不会往后台传递的,因此,保存数据的时候,该字段的数据是不会存到数据库中的.(待确认,字段中增加默认值,保存) (2)视图界面,注释的时候, ...

  10. oracle查看当前用户,数据库实例

    #sysdba用户登录[oracle@oracle ~]$ sqlplus / as sysdba #查看当前用户sql>show user; #查看当前数据库实例sql>show par ...