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

2
4 2 0 1 0
50 50
4 1 0 2 1
100

Sample Output

8.14
2.00 一个黑衣人在0到n-1的n个时间点里无限穿梭,他需要从 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]] 就可以了。
 /*
.
';;;;;.
'!;;;;;;!;`
'!;|&#@|;;;;!:
`;;!&####@|;;;;!:
.;;;!&@$$%|!;;;;;;!'.`:::::'.
'!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
:!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
`!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
:@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
`!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|.
%&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##!
!%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&:
':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%.
!@|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&'
.:%#&!;;;;;;;;;;;;;;!%|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;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
'%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
.!%%&%'`|$; :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e2 + ;
const int maxm = 1e5 + ;
const int mod = 1e9 + ;
const ll INF = 1e18 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
using namespace std; int n, m;
int cas, tol, T;
int src, des; int has[maxn];
double x[maxn];
double p[maxn];
double a[maxn][maxn]; void init() {
tol = ;
mes(a, );
mes(x, );
mes(p, );
mes(has, -);
} void bfs(int src) {
queue<int> q;
while(!q.empty())
q.pop();
q.push(src);
has[src] = tol++;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i=; i<=m; i++) {
if(fabs(p[i]) < eps) continue;
int v = (u + i) % n;
if(has[v] == -) {
has[v] = tol++;
q.push(v);
}
}
}
} void build() {
for(int i=; i<n; i++) {
if(has[i] == -) continue;
double ans = 0.0;
a[has[i]][has[i]] = 1.0;
if(i == des || i == n-des) {
x[has[i]] = ;
continue;
}
for(int j=; j<=m; j++) {
int v = (i + j) % n;
a[has[i]][has[v]] -= p[j];
ans += j * p[j];
}
x[has[i]] = ans;
}
} int gauss(int equ, int var) {
int i, j, k, col, max_r;
for(k=, col=; k<equ && col<var; k++, col++) {
max_r = k;
for(i=k+; i<equ; i++) {
if(fabs(a[i][col]) > fabs(a[max_r][col])) {
max_r = i;
}
}
if(fabs(a[max_r][col] < eps)) return ;
if(k != max_r) {
swap(a[k], a[max_r]);
swap(x[k], x[max_r]);
}
x[k] /= a[k][col];
for(j=col+; j<var; j++) {
a[k][j] /= a[k][col];
}
a[k][col] = ;
for(i=; i<equ; i++) {
if(i != k) {
x[i] -= x[k] * a[i][k];
for(j=col+; j<var; j++) {
a[i][j] -= a[k][j] * a[i][col];
}
a[i][col] = ;
}
}
}
return ;
} int main() {
scanf("%d", &T);
while(T--) {
init();
int D;
scanf("%d%d%d%d%d", &n, &m, &des, &src, &D);
for(int i=; i<=m; i++) {
scanf("%lf", &p[i]);
p[i] /= ;
}
if(src == des) {
printf("0.00\n");
continue;
}
n = * n - ;
if(D == )
src = n - src;
bfs(src);
if(has[des] == - && has[n - des] == -) {
printf("Impossible !\n");
continue;
}
build();
if(gauss(tol, tol)) {
printf("%.2f\n", x[has[src]]);
} else {
printf("Impossible !\n");
}
}
return ;
}

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. OpenTK学习笔记

    OpenGL定义 OpenGL被定义为"图形硬件的一种软件接口".实质上是3D图形和模型库,具有高度可移植性,具有非常快的速度. OpenGL架构 术语pipeline常用于阐述彼 ...

  2. Java 图片爬虫,java打包jar文件

    目录 1. Java 图片爬虫,制作 .jar 文件 spider.java 制作 jar 文件 添加执行权限 1. Java 图片爬虫,制作 .jar 文件 spider.java spider.j ...

  3. 前后端分离djangorestframework—— 接入微信模板消息推送

    微信 什么是微信也不多说,跟前面的支付宝一样的 微信支付 微信支付也有个沙箱环境,沙箱环境官方文档 由文档中那句很显眼的话所得,即使是测试环境也需要真实的商户号,所以这个就没法想支付宝那样用沙箱账号来 ...

  4. python 迭代器协议和生成器

    一.什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个stoplteration异常,以终止迭代(只能往后走,不能往前退) 2.可迭代 ...

  5. Tree 树形结构

    一.树的基本概念 (1)树(Tree)的概念:树是一种递归定义的数据结构,是一种重要的非线性数据结构. 树可以是一棵空树,它没有任何的结点:也可以是一棵非空树,至少含有一个结点. (2)根(Root) ...

  6. Python开发【socket篇】解决粘包

    客户端 import os import json import struct import socket sk = socket.socket() sk.connect(('127.0.0.1',8 ...

  7. HttpServletResponse简单理解

    Web服务器收到一个http请求,会针对每个请求创建一个HttpServletRequest和HttpServletResponse对象,从客户端取数据用HttpServletRequest,向客户端 ...

  8. 获取与esp8266连接的客户端的Mac地址 IP 端口 控制停止等问题

    两个关键的库 ESP8266WebServer.h WiFiClient.h ESP8266WiFiAP.cpp C:\Users\dongdong\Desktop\Arduino-master\li ...

  9. Arduino内部网页代理,网页穿透,公网访问Arduino内部网页

    #include <ESP8266WiFi.h> const char* id     = "id";  //http://www.mcunode.com/proxy/ ...

  10. Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures

    参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...