PvZ once again

Time Limit: 2000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

Type:

None

 

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. 1
  2. 5 9 5 3

Sample Output

  1. 0.0000 0.5000 0.0000 0.5000 0.0000

Source

 
 
解题思路:求出概率转移矩阵,因为要转移m*k次,即(原始概率矩阵B)*(概率转移矩阵A)m*k,所以根据矩阵相乘的结合律,所以可以让概率转移矩阵A先乘m*k次,因为B矩阵为所以最后只需用B矩阵乘以转移矩阵,输出乘以后的矩阵的第一行。
 
 

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <math.h>
  10. #include <string>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. using namespace std;
  14. const int maxn = 1e5+300;
  15. int n;
  16. struct Matrix{
  17. double mat[25][25];
  18. Matrix(){
  19. for(int i = 1; i < 25; i++){
  20. for(int j = 1; j < 25; j++){
  21. mat[i][j] = 0.0;
  22. }
  23. }
  24. }
  25. void mem(){
  26. for(int i = 1; i <= n; i++){
  27. for(int j = 1; j <= n; j++){
  28. mat[i][j] = 0.0;
  29. }
  30. }
  31. }
  32. void unit(){
  33. for(int i = 1; i < 25; i++)
  34. mat[i][i] = 1.0;
  35. }
  36. Matrix operator *(const Matrix &rhs)const{
  37. Matrix ret;
  38. for(int i = 1; i <= n; i++){
  39. for(int j = 1; j <= n; j++){
  40. ret.mat[i][j] = 0.0;
  41. for(int k = 1; k <= n; k++){
  42. ret.mat[i][j] += mat[i][k]*rhs.mat[k][j];
  43. }
  44.  
  45. }
  46. }
  47. return ret;
  48. }
  49. };
  50. void deg(const Matrix &rhs){
  51. for(int i = 1; i <= n; i++){
  52. for(int j = 1; j <= n; j++){
  53. printf("%.4lf ",rhs.mat[i][j]);
  54. }puts("");
  55. }
  56. }
  57.  
  58. Matrix & Quick( Matrix &p, int k){
  59. Matrix ret;
  60. ret.unit();
  61. while( k ){
  62. if(k&1){
  63. ret = ret*p;
  64. }
  65. k >>= 1;
  66. p = p*p;
  67. }
  68. p = ret;
  69. return p;
  70. }
  71. int main(){
  72. int T, m, k, s;
  73. scanf("%d",&T);
  74. while(T--){
  75. scanf("%d%d%d%d",&n,&m,&k,&s);
  76. Matrix ans, trans;
  77. ans.mem();
  78. trans.mem();
  79. for(int i = 1; i <= n; i++){
  80. if( i == 1 ){
  81. trans.mat[i][2] = 1.0;
  82. }else if( i == n ){
  83. trans.mat[i][n-1] = 1.0;
  84. }else{
  85. trans.mat[i][i-1] = 0.5;
  86. trans.mat[i][i+1] = 0.5;
  87. }
  88. }
  89. if(n == 1){
  90. puts("1.0000"); continue;
  91. }
  92. trans = Quick(trans,m*k);
  93. // deg(trans);
  94. ans.mat[1][s] = 1.0;
  95. ans = ans*trans;
  96. printf("%.4lf",ans.mat[1][1]);
  97. for(int i = 2; i <= n; i++)
  98. printf(" %.4lf",ans.mat[1][i]);
  99. puts("");
  100. }
  101. }

  

图文详解:

假设以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——————【矩阵快速幂】的更多相关文章

  1. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  2. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  3. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  4. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  5. 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 ...

  6. 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 输 ...

  7. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  8. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  9. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

随机推荐

  1. Android 学习笔记 文本文件的读写操作

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  2. java中int转String 固定位数 不足补零

    转载自:http://ych0108.iteye.com/blog/2174134 String.format("%010d", 25); //25为int型 0代表前面要补的字符 ...

  3. 「BZOJ 2342」「SHOI 2011」双倍回文「Manacher」

    题意 记\(s_R\)为\(s\)翻转后的串,求一个串最长的形如\(ss_Rss_R\)的子串长度 题解 这有一个复杂度明显\(O(n)\)的做法,思路来自网上某篇博客 一个双倍回文串肯定当且仅当本身 ...

  4. 【bzoj5093】 [Lydsy1711月赛]图的价值 组合数+斯特林数+NTT

    Description "简单无向图"是指无重边.无自环的无向图(不一定连通). 一个带标号的图的价值定义为每个点度数的k次方的和. 给定n和k,请计算所有n个点的带标号的简单无向 ...

  5. 【bzoj3309】DZY Loves Math 莫比乌斯反演+线性筛

    Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b, ...

  6. luoguP3648 [APIO2014]序列分割

    https://www.luogu.org/problemnew/show/P3648 同bzoj3675 这题斜率优化+滚动数组就可以了qwq 因为我是在飞机上瞎bb的式子,所以可能会和别的题解的式 ...

  7. CBoard 汉化以及元数据存储配置

    汉化配置如图位置: 元数据存储配置为Mysql数据库的位置 版权声明:本文为博主原创文章,需要转载请注明出处. [置顶]Cboard 系列随笔

  8. ios网络 -- HTTP请求 and 文件下载/断点下载

    一:请求 http://www.jianshu.com/p/8a90aa6bad6b 二:下载 iOS网络--『文件下载.断点下载』的实现(一):NSURLConnection http://www. ...

  9. java基础_01

    一.java中的数据类型 1.基本数据类型:四类八种 byte(1),boolean(1),short(2),char(2),int(4),float(4),long(8),double(8); 2. ...

  10. ASP.NET后台取html控件值方式

    1.Request.Form[“cbName”]: 可以在后台取到所有为name 为的控件的value值 2.可以通过 把html控件的值付给HiddenField,然后后台调用 3.就是自定义属性 ...