Time travel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1480    Accepted Submission(s): 327

Problem Description

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.
 
Input
There 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.
 
Output
For 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
 
Source

题意:一个人在数轴上来回走,以pi的概率走i步i∈[1, m],给定n(数轴长度),m,e(终点),s(起点),d(方向),求从s走到e经过的点数期望

解析:设E[x]是人从x走到e经过点数的期望值,显然对于终点有:E[e] = 0

一般的:E[x] = sum((E[x+i]+i) * p[i])(i∈[1, m]) (走i步经过i个点,所以是E[x+i]+i)

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std; const int maxn=;
const double eps=1e-;
int map[maxn],flag[maxn];
double p[maxn],A[maxn][maxn];
int cnt,n,m,st,ed,d;
int dcmp(double x)
{
if(fabs(x)<eps) return ;
else if(x->eps) return ;
return -;
}
void swap(double &a,double &b){double t=a;a=b;b=t;} bool bfs()
{
memset(flag,-,sizeof(flag));
queue<int>Q;
cnt=;flag[st]=cnt++;
Q.push(st);
bool ret=false;
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=;i<=m;i++)
{
int v=(u+i)%(*n-);
if(dcmp(p[i])==) continue;
if(flag[v]!=-) continue;
flag[v]=cnt++;
if(map[v]==ed) ret=true;
Q.push(v);
}
}
return ret;
} void bulidmatrix()
{
memset(A,,sizeof(A));
for(int i=;i<*n-;i++)
{
if(flag[i]==-) continue;
int u=flag[i];A[u][u]=;
if(map[i]==ed){A[u][cnt]=;continue;}
for(int j=;j<=m;j++)
{
int v=(i+j)%(*n-);
if(flag[v]==-) continue;
v=flag[v];
A[u][v]-=p[j];A[u][cnt]+=p[j]*j;
}
}
} void gauss(int n)
{
int i,j,k,r;
for(i=;i<n;i++)
{
r=i;
for(j=i+;j<n;j++)
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
if(dcmp(A[r][i])==) continue;
if(r!=i) for(j=;j<=n;j++) swap(A[r][j],A[i][j]);
for(k=;k<n;k++) if(k!=i)
for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d",&n,&m,&ed,&st,&d);
for(i=;i<=m;i++){ scanf("%lf",p+i);p[i]/=;}
if(st==ed){ printf("0.00\n");continue;}
for(i=;i<n;i++) map[i]=i;
for(i=n,j=n-;i<*n-;i++,j--) map[i]=j;
if(d==) st=*n--st;
if(!bfs()){ printf("Impossible !\n");continue;}
bulidmatrix();gauss(cnt);
for(i=cnt-;i>=;i--)
{
for(j=i+;j<cnt;j++)
A[i][cnt]-=A[j][cnt]*A[i][j];
A[i][cnt]/=A[i][i];
}
printf("%.2lf\n",A[][cnt]);
}
return ;
}

hdu 4418 高斯消元求期望的更多相关文章

  1. hdu 2262 高斯消元求期望

    Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  2. hdu 4870 rating(高斯消元求期望)

    Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  3. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  4. [ACM] hdu 4418 Time travel (高斯消元求期望)

    Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...

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

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

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

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

  7. hdu 3992 AC自动机上的高斯消元求期望

    Crazy Typewriter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)

    网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...

  9. 高斯消元与期望DP

    高斯消元可以解决一系列DP序混乱的无向图上(期望)DP DP序 DP序是一道DP的所有状态的一个排列,使状态x所需的所有前置状态都位于状态x前: (通俗的说,在一个状态转移方程中‘=’左侧的状态应该在 ...

随机推荐

  1. CPP-基础:C/C++数组名与指针的区别

    2005-08-23 08:36 来源:天极网 作者:宋宝华 责任编辑:方舟·yesky 引言 指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很 ...

  2. 如何将oracle查询的结果传输给变量

    如何将oracle查询的结果传输给变量 1. sqlplus查询时的变量设置 set echo off; #控制start命令不列出命令文件中的每一命令 set feedback off; #显示由查 ...

  3. cocos2dx 使用XMLHttpRequest时回调status为0的问题

    今天使用cocos连接http访问时,使用XMLHttpRequest在pc上反问时正常的返回了status=0,但是在android上去返回status是0,看了一下底层代码, 发现status只有 ...

  4. 基于matlab的蓝色车牌定位与识别---分割

    接着上面的工作,接下去就该是进行字符分割了.考虑到为了后面的字符识别,因此在这部分需要实现的目标是需要把车牌的边框全部切除,对重新定位的车牌进行垂直方向水平方向调整,保证字符是正的.最后才是字符的分割 ...

  5. 【NOIP提高A组模拟2018.8.14】 区间

    区间加:差分数组修改 O(n)扫描,负数位置单调不减 #include<iostream> #include<cstring> #include<cstdio> # ...

  6. mysql 主从数据校验

    使用工具pt-table-checksum: /usr/bin/pt-table-checksum --user=root --password='mysqlpass' --host=127.0.0. ...

  7. 忘记root密码怎么办-单用户模式修改root密码

    忘记root密码怎么办-单用户模式修改root密码================================= 1,开机3秒内按下向下的方向键,目的是为了不让它进入系统,而是停留在开机界面. 2 ...

  8. 快速排序和快速选择(quickSort and quickSelect)算法

    排序算法:快速排序(quicksort)递归与非递归算法 TopK问题:快速选择(quickSelect)算法 import java.util.*; import java.lang.*; publ ...

  9. chrome浏览器跳过(忽略)所有的js断点

    在调试程序时我们经常通过打断点的方式来跟踪代码的执行流程,所以可能会在很多时候打很多断点,当我们知道了程序大概的执行流程之后,这时候断点就不太需要了.但是我们又不想马上把所有的断点清除掉,因为我们打的 ...

  10. Mac远程访问Ubuntu

    MacOS和Ubuntu连接到同一个网络使用ping命令可以通信即可.SSH使用SSH可以很方便的在MacOS上访问Ubuntu,不过只能用命令行操作,相当于连接了Ubuntu的终端. 1. Ubun ...