Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to finish a mission by traveling through time with the Time machine. The Time machine can take agent K to some point (0 to n-1) on the timeline and when he gets to the end of the time line he will come back (For example, there are 4 time points, agent K will go in this way 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, ...). But when agent K gets into the Time machine he finds it has broken, which make the Time machine can't stop (Damn it!). Fortunately, the time machine may get recovery and stop for a few minutes when agent K arrives at a time point, if the time point he just arrive is his destination, he'll go and finish his mission, or the Time machine will break again. The Time machine has probability Pk% to recover after passing k time points and k can be no more than M. We guarantee the sum of Pk is 100 (Sum(Pk) (1 <= k <= M)==100). Now we know agent K will appear at the point X(D is the direction of the Time machine: 0 represents going from the start of the timeline to the end, on the contrary 1 represents going from the end. If x is the start or the end point of the time line D will be -1. Agent K want to know the expectation of the amount of the time point he need to pass before he arrive at the point Y to finish his mission. 
If finishing his mission is impossible output "Impossible !" (no quotes )instead. 

InputThere is an integer T (T <= 20) indicating the cases you have to solve. The first line of each test case are five integers N, M, Y, X .D (0< N,M <= 100, 0 <=X ,Y < 100 ). The following M non-negative integers represent Pk in percentile. 
OutputFor each possible scenario, output a floating number with 2 digits after decimal point 
If finishing his mission is impossible output one line "Impossible !" 
(no quotes )instead. 
Sample Input

  1. 2
  2. 4 2 0 1 0
  3. 50 50
  4. 4 1 0 2 1
  5. 100

Sample Output

  1. 8.14
  2. 2.00
  3.  
  4. 一个黑衣人在0n-1n个时间点里无限穿梭,他需要从 x 位置到 y 位置,时间穿梭器每传送 k 次,最多M次,就有 Pk 的概率停下一次,如果停下的位置正好是 y 那么黑衣人到达目的地。
    然后给出的 N 个点,M次传送次数,目的地y,出发地x,方向D D 0 时从左到右走,D 1 时,从右往左走。
    对于方向,可以把 N 个点翻倍,比如 N = 5,可以看成0 1 2 3 4 5 4 3 2 1 假设我x = 1 D = 0,那么就是从下标 1 开始走,如果x = 1, D = 1,那么我就是从下标 2*n-2-1 开始走,并且这么走,都只要看成往右走。
    然后对开始位置进行 bfs ,把可以从开始位置走的点找出来,并且对这些点标号。
    然后对于列方程。令dp[i] 表示从 i 位置走出的期望。
    1. i == y, dp[i] = 0.
    2. i != y, dp[i] = Σ((dp[i+j] + j) * p[j])
    = Σ(dp[i+j] * p[j]) + p[j] * j
    dp[i] - Σ(dp[i+j] * p[j]) = p[j] * j
    通过这个式子对可以走到的点列方程
    列方程时 a[has[i]][has[v]] -= p[j]; a[has[i]][has[v]] = -p[j]; 是不一样的,因为我需要对 n 取模,那么我的 v 位置可能多次走到。
    然后用高斯消元求出 x[has[x]] 就可以了。
  1. /*
  2. .
  3. ';;;;;.
  4. '!;;;;;;!;`
  5. '!;|&#@|;;;;!:
  6. `;;!&####@|;;;;!:
  7. .;;;!&@$$%|!;;;;;;!'.`:::::'.
  8. '!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
  9. :!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
  10. ;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
  11. ;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
  12. `!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
  13. :@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
  14. ;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
  15. `!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|.
  16. %&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##!
  17. !%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&:
  18. ':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%.
  19. !@|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&'
  20. .:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@:
  21. .%$;;;;;;;;;;;;;;;;;;|$$$$@&|;;;;;;;;;;;;;;;;;;;;%@%.
  22. !&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#;
  23. `%$!;;;;;;;;;;;$@|;;;;;;;;;;;;;;;;;;;;;;;;!%$@#@|.
  24. .|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%$$$$$@#|.
  25. ;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|.
  26. |##$|!;;;;;;::'':;;;;;;;;;;;;;!%$$$@#@;
  27. ;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|`
  28. .%##@|;;;;:::''''''''''::;!%&##$'
  29. `$##@$$@@&|!!;;;:'''''::::;;;;;|&#%.
  30. ;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@:
  31. .%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%.
  32. :@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;`
  33. `%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&'
  34. |##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
  35. :;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$: !@#$'
  36. |##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%: '%%!%&;
  37. |&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&;
  38. |@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%`
  39. !@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&:
  40. '$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&:
  41. |#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$'
  42. |&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@!
  43. `%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
  44. !&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
  45. '%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
  46. .!%%&%'`|$; :|$#%|@#&;%#%.
  47. */
  48. #include <map>
  49. #include <set>
  50. #include <list>
  51. #include <ctime>
  52. #include <cmath>
  53. #include <stack>
  54. #include <queue>
  55. #include <string>
  56. #include <vector>
  57. #include <cstdio>
  58. #include <bitset>
  59. #include <cstdlib>
  60. #include <cstring>
  61. #include <iostream>
  62. #include <algorithm>
  63. #define lowbit(x) x & (-x)
  64. #define mes(a, b) memset(a, b, sizeof a)
  65. #define fi first
  66. #define se second
  67. #define pii pair<int, int>
  68. #define INOPEN freopen("in.txt", "r", stdin)
  69. #define OUTOPEN freopen("out.txt", "w", stdout)
  70.  
  71. typedef unsigned long long int ull;
  72. typedef long long int ll;
  73. const int maxn = 2e2 + ;
  74. const int maxm = 1e5 + ;
  75. const int mod = 1e9 + ;
  76. const ll INF = 1e18 + ;
  77. const int inf = 0x3f3f3f3f;
  78. const double pi = acos(-1.0);
  79. const double eps = 1e-;
  80. using namespace std;
  81.  
  82. int n, m;
  83. int cas, tol, T;
  84. int src, des;
  85.  
  86. int has[maxn];
  87. double x[maxn];
  88. double p[maxn];
  89. double a[maxn][maxn];
  90.  
  91. void init() {
  92. tol = ;
  93. mes(a, );
  94. mes(x, );
  95. mes(p, );
  96. mes(has, -);
  97. }
  98.  
  99. void bfs(int src) {
  100. queue<int> q;
  101. while(!q.empty())
  102. q.pop();
  103. q.push(src);
  104. has[src] = tol++;
  105. while(!q.empty()) {
  106. int u = q.front();
  107. q.pop();
  108. for(int i=; i<=m; i++) {
  109. if(fabs(p[i]) < eps) continue;
  110. int v = (u + i) % n;
  111. if(has[v] == -) {
  112. has[v] = tol++;
  113. q.push(v);
  114. }
  115. }
  116. }
  117. }
  118.  
  119. void build() {
  120. for(int i=; i<n; i++) {
  121. if(has[i] == -) continue;
  122. double ans = 0.0;
  123. a[has[i]][has[i]] = 1.0;
  124. if(i == des || i == n-des) {
  125. x[has[i]] = ;
  126. continue;
  127. }
  128. for(int j=; j<=m; j++) {
  129. int v = (i + j) % n;
  130. a[has[i]][has[v]] -= p[j];
  131. ans += j * p[j];
  132. }
  133. x[has[i]] = ans;
  134. }
  135. }
  136.  
  137. int gauss(int equ, int var) {
  138. int i, j, k, col, max_r;
  139. for(k=, col=; k<equ && col<var; k++, col++) {
  140. max_r = k;
  141. for(i=k+; i<equ; i++) {
  142. if(fabs(a[i][col]) > fabs(a[max_r][col])) {
  143. max_r = i;
  144. }
  145. }
  146. if(fabs(a[max_r][col] < eps)) return ;
  147. if(k != max_r) {
  148. swap(a[k], a[max_r]);
  149. swap(x[k], x[max_r]);
  150. }
  151. x[k] /= a[k][col];
  152. for(j=col+; j<var; j++) {
  153. a[k][j] /= a[k][col];
  154. }
  155. a[k][col] = ;
  156. for(i=; i<equ; i++) {
  157. if(i != k) {
  158. x[i] -= x[k] * a[i][k];
  159. for(j=col+; j<var; j++) {
  160. a[i][j] -= a[k][j] * a[i][col];
  161. }
  162. a[i][col] = ;
  163. }
  164. }
  165. }
  166. return ;
  167. }
  168.  
  169. int main() {
  170. scanf("%d", &T);
  171. while(T--) {
  172. init();
  173. int D;
  174. scanf("%d%d%d%d%d", &n, &m, &des, &src, &D);
  175. for(int i=; i<=m; i++) {
  176. scanf("%lf", &p[i]);
  177. p[i] /= ;
  178. }
  179. if(src == des) {
  180. printf("0.00\n");
  181. continue;
  182. }
  183. n = * n - ;
  184. if(D == )
  185. src = n - src;
  186. bfs(src);
  187. if(has[des] == - && has[n - des] == -) {
  188. printf("Impossible !\n");
  189. continue;
  190. }
  191. build();
  192. if(gauss(tol, tol)) {
  193. printf("%.2f\n", x[has[src]]);
  194. } else {
  195. printf("Impossible !\n");
  196. }
  197. }
  198. return ;
  199. }

Time travel HDU - 4418(高斯消元)的更多相关文章

  1. [置顶] hdu 4418 高斯消元解方程求期望

    题意:  一个人在一条线段来回走(遇到线段端点就转变方向),现在他从起点出发,并有一个初始方向, 每次都可以走1, 2, 3 ..... m步,都有对应着一个概率.问你他走到终点的概率 思路: 方向问 ...

  2. HDU 4418 高斯消元解决概率期望

    题目大意: 一个人在n长的路径上走到底再往回,走i步停下来的概率为Pi , 求从起点开始到自己所希望的终点所走步数的数学期望 因为每个位置都跟后m个位置的数学期望有关 E[i] = sigma((E[ ...

  3. hdu 4418 高斯消元求期望

    Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU 2827 高斯消元

    模板的高斯消元.... /** @Date : 2017-09-26 18:05:03 * @FileName: HDU 2827 高斯消元.cpp * @Platform: Windows * @A ...

  5. HDU-4418 Time travel 概率DP,高斯消元

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4418 题意:简单来说就是给你1个环(n - 1 , n - 2 …… 0 ,1 , 2 , 3 …… ...

  6. hdu 3915 高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=3915 这道题目是和博弈论挂钩的高斯消元.本题涉及的博弈是nim博弈,结论是:当先手处于奇异局势时(几堆石子数相互 ...

  7. HDU 3359 高斯消元模板题,

    http://acm.hdu.edu.cn/showproblem.php?pid=3359 题目的意思是,由矩阵A生成矩阵B的方法是: 以a[i][j]为中心的,哈曼顿距离不大于dis的数字的总和 ...

  8. HDU4418 Time travel(期望dp 高斯消元)

    题意 题目链接 Sol mdzz这题真的太恶心了.. 首先不难看出这就是个高斯消元解方程的板子题 \(f[x] = \sum_{i = 1}^n f[to(x + i)] * p[i] + ave\) ...

  9. hdu 5088 高斯消元n堆石子取k堆石子使剩余异或值为0

    http://acm.hdu.edu.cn/showproblem.php?pid=5088 求能否去掉几堆石子使得nim游戏胜利 我们可以把题目转化成求n堆石子中的k堆石子数异或为0的情况数.使用x ...

  10. HDU 3364 高斯消元

    Lanterns Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

随机推荐

  1. mssql sqlserver 表增加列后,视图不会自动更新相关列的两种解决方法分享

    摘要: 今天对物理数据表,进行增加列操作后,程序一直显示无法找到相应列,通过仔细比对发现,视图中无相应列更新,下文将具体的解决方法分享如下: 例: create view vw_test as sel ...

  2. QQ邮箱开启SMTP方法如何授权

    步骤一: 在打开的邮箱中心,进入设置 步骤二 从邮箱设置中心,进入帐户 步骤三 在这里可以看到POP3/SMTP服务被关闭了,因此客户端会收不到邮件,我们来开启它,查看该服务为已开启时,就可以用客户端 ...

  3. SQLServer之函数简介

    用户定义函数定义 与编程语言中的函数类似,SQL Server 用户定义函数是接受参数.执行操作(例如复杂计算)并将操作结果以值的形式返回的例程. 返回值可以是单个标量值或结果集. 用户定义函数准则 ...

  4. python3 Counter类(计数器)

    Counter(计数器):用于追踪值的出现次数 Counter类继承dict类,所以它能使用dict类里面的方法 创建一个Counter类 import collections obj = colle ...

  5. python——python3.6环境搭建(Windows10,64位)

    1.python软件资源下载 1.1 打开python官网地址:https://www.python.org 1.2 根据自己电脑的设置选择下载合适的python3.6.2 1.3 此处选择windo ...

  6. LeetCode算法题-Longest Uncommon Subsequence I(Java实现)

    这是悦乐书的第252次更新,第265篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第119题(顺位题号是521).给定一组两个字符串,您需要找到这组两个字符串中最长的不同 ...

  7. 1.2 NCE22 By heart

    Some plays are so successful that they run/are performed/ for years on end/successively/in a row/con ...

  8. RESTful API规范

    1. 域名 应该尽量将API部署在专用的域名下. https://api.example.com 如果确定API简单,不会有进一步的括在,可以考虑放在主域名之下. https://example.or ...

  9. Python开发【第一篇】基础题目一

    1.求1-2+3-4+5.....99的所有数的和 n = 1 s = 0 while n<100: temp = n%2 if temp == 0: #偶数 s = s-n else: s = ...

  10. Offset Management For Apache Kafka With Apache Spark Streaming

    An ingest pattern that we commonly see being adopted at Cloudera customers is Apache Spark Streaming ...