洛谷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 \ ...
随机推荐
- 技术文档生成工具:appledoc
做项目一般都会要求写技术文档,特别是提供SDK或者基础组件的.如果手写这类技术文档的话,工作量比编写代码也少不了多少.比如 Java 语言本身就自带 javadoc 命令,可以从源码中抽取文档.本篇我 ...
- 牛客JS编程大题(一)
1.找出元素 item 在给定数组 arr 中的位置 function indexOf(arr,item){ return arr.indexOf(item);} 2.计算给定数组 arr 中所有元素 ...
- 腾讯云播放器更新——TCplayer
概述 最近腾讯云播放器进行了更新,增加了TCplayer,支持点播播放.由于工作需要,了解了一下TCplayer,把心得记录下来,供以后开发时参考,相信对其他人也有用. 参考文档: TCPlayer开 ...
- PHP、JS、Python,数据库 获取今天是星期几了?[开发篇]
额,这个看起来是一个好简单的问题,但是真正到自己去一行行写的时候,又给忘了,妈蛋.有空就看看吧.今天是星期几?下面就来看看几种不同语言的实现吧! PHP语言 输出当前时间: echo date('Y- ...
- ElasticSearch权威指南学习(索引管理)
创建索引 当我们需要确保索引被创建在适当数量的分片上,在索引数据之前设置好分析器和类型映射. 手动创建索引,在请求中加入所有设置和类型映射,如下所示: PUT /my_index { "se ...
- Shell - 简明Shell入门15 - 调试(Debug)
示例脚本及注释 #!/bin/bash -x for filename in t1 t2 t3 do touch $filename.txt echo "Create new file: $ ...
- django rest framework serializers解读
serializers是什么?官网是这样的"Serializers allow complex data such as querysets and model instances to b ...
- mybatis随笔五之Executor
在上一篇文章我们分析到了mapper接口方法的实现实际上是交由代理类来实现的,并最终调用Executor来查询,接下来我们对executor.query(ms, wrapCollection(para ...
- 又拍云 Node.js 实现文件上传、删除
Node.js 服务端 使用 Node.js + Express.js 实现 服务端 const express = require("express"); const app = ...
- 从零开始学 Web 之 Ajax(六)jQuery中的Ajax
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...