BNU29139——PvZ once again——————【矩阵快速幂】
PvZ once again
64-bit integer IO format: %lld Java class name: Main
None
Graph Theory
2-SAT
Articulation/Bridge/Biconnected Component
Cycles/Topological Sorting/Strongly Connected Component
Shortest Path
Bellman Ford
Dijkstra/Floyd Warshall
Euler Trail/Circuit
Heavy-Light Decomposition
Minimum Spanning Tree
Stable Marriage Problem
Trees
Directed Minimum Spanning Tree
Flow/Matching
Graph Matching
Bipartite Matching
Hopcroft–Karp Bipartite Matching
Weighted Bipartite Matching/Hungarian Algorithm
Flow
Max Flow/Min Cut
Min Cost Max Flow
DFS-like
Backtracking with Pruning/Branch and Bound
Basic Recursion
IDA* Search
Parsing/Grammar
Breadth First Search/Depth First Search
Advanced Search Techniques
Binary Search/Bisection
Ternary Search
Geometry
Basic Geometry
Computational Geometry
Convex Hull
Pick's Theorem
Game Theory
Green Hackenbush/Colon Principle/Fusion Principle
Nim
Sprague-Grundy Number
Matrix
Gaussian Elimination
Matrix Exponentiation
Data Structures
Basic Data Structures
Binary Indexed Tree
Binary Search Tree
Hashing
Orthogonal Range Search
Range Minimum Query/Lowest Common Ancestor
Segment Tree/Interval Tree
Trie Tree
Sorting
Disjoint Set
String
Aho Corasick
Knuth-Morris-Pratt
Suffix Array/Suffix Tree
Math
Basic Math
Big Integer Arithmetic
Number Theory
Chinese Remainder Theorem
Extended Euclid
Inclusion/Exclusion
Modular Arithmetic
Combinatorics
Group Theory/Burnside's lemma
Counting
Probability/Expected Value
Others
Tricky
Hardest
Unusual
Brute Force
Implementation
Constructive Algorithms
Two Pointer
Bitmask
Beginner
Discrete Logarithm/Shank's Baby-step Giant-step Algorithm
Greedy
Divide and Conquer
Dynamic Programming
Tag it!
植物大战僵尸算个out的游戏了,原谅被出题逼疯了的跑来挖坟了。
会玩的请无视这一段直接看题目{
游戏中僵尸向你的房子进发,吃掉沿途遇到的植物进入你的房子 你就死翘了
你在土地上种植各种植物来攻击阻挡僵尸
手推车:放置在终点,僵尸走到面前会启动推倒一整行的僵尸
大蒜:可种植的一种植物,发出恶心的气味,僵尸咬了一口就会换到邻近的另一行(如果有相邻两行,那么移动到另外两行概率是相等的)
南瓜:单纯的肉盾 被僵尸啃的
耐久度K: 植物被咬了K口后被僵尸吃掉
如有其他对游戏的不理解请clarify
}
问题是这样的:
我们的院子变成了N行M列的,而且种满了大蒜(耐久度K)(图是我盗了 我不会这么无聊的)coming的僵尸只有一只(然而这只僵尸貌似发生了变异,它每啃一口植物,同一列相同种类的植物也被啃掉一口,一口一排的样子恩恩),初始位置在第S行,因为没有放置攻击性的植物,所以僵尸就一路吃了,于是出题者很想知道僵尸死在自上而下1-N号手推车的概率各是多少
(无视掉图中的南瓜,实际上对僵尸行走没有影响。。)
Input
一个整数T(表示T组数据)
接下来的T组数据
每组给定四个整数 N M K S
数据范围
T<=1000
0<N<=20
0<M<=1000
0<K<=1000
1<=S<=N
Output
对于每组数据输出一行N个4位小数 用空格隔开 表示僵尸死在相应行的概率 行末没有空格
Sample Input
1
5 9 5 3
Sample Output
0.0000 0.5000 0.0000 0.5000 0.0000
Source
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int maxn = 1e5+300;
int n;
struct Matrix{
double mat[25][25];
Matrix(){
for(int i = 1; i < 25; i++){
for(int j = 1; j < 25; j++){
mat[i][j] = 0.0;
}
}
}
void mem(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
mat[i][j] = 0.0;
}
}
}
void unit(){
for(int i = 1; i < 25; i++)
mat[i][i] = 1.0;
}
Matrix operator *(const Matrix &rhs)const{
Matrix ret;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
ret.mat[i][j] = 0.0;
for(int k = 1; k <= n; k++){
ret.mat[i][j] += mat[i][k]*rhs.mat[k][j];
} }
}
return ret;
}
};
void deg(const Matrix &rhs){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
printf("%.4lf ",rhs.mat[i][j]);
}puts("");
}
} Matrix & Quick( Matrix &p, int k){
Matrix ret;
ret.unit();
while( k ){
if(k&1){
ret = ret*p;
}
k >>= 1;
p = p*p;
}
p = ret;
return p;
}
int main(){
int T, m, k, s;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&n,&m,&k,&s);
Matrix ans, trans;
ans.mem();
trans.mem();
for(int i = 1; i <= n; i++){
if( i == 1 ){
trans.mat[i][2] = 1.0;
}else if( i == n ){
trans.mat[i][n-1] = 1.0;
}else{
trans.mat[i][i-1] = 0.5;
trans.mat[i][i+1] = 0.5;
}
}
if(n == 1){
puts("1.0000"); continue;
}
trans = Quick(trans,m*k);
// deg(trans);
ans.mat[1][s] = 1.0;
ans = ans*trans;
printf("%.4lf",ans.mat[1][1]);
for(int i = 2; i <= n; i++)
printf(" %.4lf",ans.mat[1][i]);
puts("");
}
}
图文详解:
假设以n,m,k,s分别为5,9,9,3为例。P、PP、PPP分别代表咬1、2、3口后的概率,只是对于耐久度为9时,他们是在同一列的,只不过为了表示,所以这样给出,不要误解。PP1=0* P1+0.5* P2+0* P3+0* P4+ 0 * P5
PP2=1* P1+0 * P2+0.5*P3+0* P4+0* P5
PP3=0* P1+0.5* P2+0* P3+0.5*P4+0* P5
PP4=0* P1+0* P2+0.5* P3+0* P4+1* P5
PP5=0* P1+0* P2+0* P3+0.5*P4+0* P5
由上表可以看出,后一个列概率矩阵PP由前一个概率矩阵P乘以某一个矩阵得到。我们假设该某矩阵为A即:
0 |
1 |
0 |
0 |
0 |
0.5 |
0 |
0.5 |
0 |
0 |
0 |
0.5 |
0 |
0.5 |
0 |
0 |
0 |
0.5 |
0 |
0.5 |
0 |
0 |
0 |
1 |
0 |
将列概率矩阵转为行概率矩阵B:
P1 |
P2 |
P3 |
P4 |
P5 |
用B*A得到下一个行概率矩阵B‘。同时由于矩相乘具有结合律,所以我们可以用矩阵快速幂来先求出转移m次的转移矩阵Am*k,然后用原始矩阵B*Am*k即可求得。
BNU29139——PvZ once again——————【矩阵快速幂】的更多相关文章
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 51nod 1126 矩阵快速幂 水
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
- hdu2604(递推,矩阵快速幂)
题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
随机推荐
- mvc 高并发解决方案之一---存储过程
MVC用户访问多线程,一般的lock是无法造成单例的. 存储过程既是一种解决方案,先来看看存储过程优缺点: A. 存储过程允许标准组件式编程 存储过程创建后可以在程序中被多次调用执行,而不必重新编写该 ...
- NSTimeZone时区
前言 NSTimeZone 表示时区信息. 1.NSTimeZone 时区的创建 NSTimeZone *zone1 = [[NSTimeZone alloc] init]; // 根据时区名称创建 ...
- 深入 Nginx:我们是如何为性能和规模做设计的
NGINX 在网络应用中表现超群,在于其独特的设计.许多网络或应用服务器大都是基于线程或者进程的简单框架,NGINX突出的地方就在于其成熟的事件驱动框架,它能应对现代硬件上成千上万的并发连接. NGI ...
- 题解 P1876 【开灯】
题目链接 编者说得对 一道很明显的数学题,相信大家小学都做过. 通俗一点,就是找因数为奇数个的数.而这一类的数.明显的是平方数. 所以就是找n以内的平方数. 废话少说,直接上题解. #include& ...
- luoguP2495 [SDOI2011]消耗战
https://www.luogu.org/problemnew/show/P2495 Dp 方程很显然 设 Dp[u] 表示--使 u 不与其子树中任意一个关键点联通的最小代价 设 w[a, b] ...
- iOS 开发之 GCD 不同场景使用
header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...
- 用shell脚本安装apache
我们首先创建一个文件为test.sh,执行此文件的方法有以下四种方式: 1../test.sh(必须chmod赋予执行权限) 2.. test.sh 3.sourse test.sh 4.[shell ...
- winform跨线程问题(有参数和无参数)
1.invoke是同步线程 using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...
- [JSOI2009]计数问题 二维树状数组BZOJ 1452
题目描述 一个n*m的方格,初始时每个格子有一个整数权值.接下来每次有2种操作: 改变一个格子的权值: 求一个子矩阵中某种特定权值出现的个数. 输入输出格式 输入格式: 第一行有两个数N,M. 接下来 ...
- ansible基本模块-server
ansible XXX -m service -a "name=XXX state=started enabled=yes"