Bzoj2510 弱题(矩阵快速幂)
题面(权限题)
题解
一道概率$dp$,可以设$f[i][j]$表示第$i$次操作后,标号为$j$的小球的期望个数,那么有:
$$
\begin{aligned}
&f[i][j]=(1-\frac 1m)f[i-1][j]+\frac1mf[i-1][j-1](1\leq j\leq n) \
&f[i][0]=(1-\frac 1m)f[i-1][j]+\frac1mf[i-1][n]
\end{aligned}
$$
这样的话转移可以写成矩阵的形式(假设有$4$个小球):
$$
\begin{aligned}
&\begin{bmatrix}
f[i-1][1]&f[i-1][2]&f[i-1][3]&f[i-1][4]
\end{bmatrix}
\times
\begin{bmatrix}
1-\frac 1m&\frac 1m&0&0\
0&1-\frac 1m&\frac 1m&0\
0&0&1-\frac 1m&\frac 1m\
\frac 1m&0&0&1-\frac 1m
\end{bmatrix}
\=
&\begin{bmatrix}
f[i][1]&f[i][2]&f[i][3]&f[i][4]
\end{bmatrix}
\end{aligned}
$$
可以发现转移矩阵也是一个循环矩阵,也就是说,可以$O(n^2log_2k)$做。
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
typedef long long ll;
template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}
const int N = 1e3 + 10;
int n, m, k; double S[N], T[N], tmp[N];
void mul(double S[], double T[]) {
memset(tmp, 0, sizeof tmp);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
tmp[(i + j - 2) % n + 1] += S[i] * T[j];
memcpy(S, tmp, sizeof tmp);
}
int main () {
read(n), read(m), read(k);
for(int i = 1; i <= n; ++i) scanf("%lf", S + i);
T[1] = 1 - 1.0 / m, T[2]= 1.0 / m;
for(; k; k >>= 1, mul(T, T)) if(k & 1) mul(S, T);
for(int i = 1; i <= n; ++i) printf("%.3lf\n", S[i]);
return 0;
}
Bzoj2510 弱题(矩阵快速幂)的更多相关文章
- BZOJ 2510: 弱题( 矩阵快速幂 )
每进行一次, 编号为x的数对x, 和(x+1)%N都有贡献 用矩阵快速幂, O(N3logK). 注意到是循环矩阵, 可以把矩阵乘法的复杂度降到O(N2). 所以总复杂度就是O(N2logK) --- ...
- BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...
- 【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)
Description 存在如下递推式: F(n+1)=A1*F(n)+A2*F(n-1)+-+An*F(1) F(n+2)=A1*F(n+1)+A2*F(n)+-+An*F(2) - 求第K项的值对 ...
- HDU5950【矩阵快速幂】
主要还是i^4化成一个(i+1)^4没遇到过,还是很基础的一题矩阵快速幂: #include <bits/stdc++.h> using namespace std; typedef lo ...
- (中等) CF 576D Flights for Regular Customers (#319 Div1 D题),矩阵快速幂。
In the country there are exactly n cities numbered with positive integers from 1 to n. In each city ...
- HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Educational Codeforces Round 13——D. Iterated Linear Function(矩阵快速幂或普通快速幂水题)
D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes input ...
- HDU 1575 矩阵快速幂裸题
题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...
- HDU 5863 cjj's string game ( 16年多校10 G 题、矩阵快速幂优化线性递推DP )
题目链接 题意 : 有种不同的字符,每种字符有无限个,要求用这k种字符构造两个长度为n的字符串a和b,使得a串和b串的最长公共部分长度恰为m,问方案数 分析 : 直觉是DP 不过当时看到 n 很大.但 ...
- luoguP3390(矩阵快速幂模板题)
链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...
随机推荐
- [Thu Summer Camp 2015]解密运算
4104: [Thu Summer Camp 2015]解密运算 Time Limit: 10 Sec Memory Limit: 512 MB Description 对于一个长度为N的字符串,我 ...
- dp ZOJ 3956
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 Course Selection System Time Limit ...
- Counting Pair
Counting Pair Time Limit: 1000 ms Memory Limit: 65535 kB Solved: 112 Tried: 1209 Submit Status Best ...
- Java学习遇到的问题
一. Java中泛型如何比较大小,继承Comparable类,然后实现其唯一的方法compareTo(): public class GenericClass<E extends Compara ...
- 一张图搞懂Spring bean的完整生命周期
一张图搞懂Spring bean的生命周期,从Spring容器启动到容器销毁bean的全过程,包括下面一系列的流程,了解这些流程对我们想在其中任何一个环节怎么操作bean的生成及修饰是非常有帮助的. ...
- Map总结
Map是键值对集合,是一对一对往上存的,要保持键的唯一性 形式:Map<K, V> 方法: 增 put(K key, V value) 若存储时Map中有相同的键,则返回原来键的值,并覆盖 ...
- Broken Necklace
Description 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个 例子: 1 2 1 2 r b b r b ...
- 数组中的each 和 jquery 中的 each
数组的实例上都有一个叫做 forEach 的方法,这个方法定义在 Array.prototype 上,所以数组的所有实例都可以使用 forEach 这个方法. forEach 方法的语法结构如下: v ...
- yii2-widget-fileinput英文文档翻译
源地址:http://plugins.krajee.com/file-input 该插件是为bootstrap开发的增强版h5文件上传插件,具有多文件预览,多文件选择等功能.该插件提供了基于boots ...
- WAMP允许外部访问的修改方法
apache配置文件httpd.conf里的 "Require local"改" Require all granted"