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. BigDecimal and double

    BigDecimal类 对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数 ...

  2. Windows系统安装docker

    下载安装包 官网 点击 Get Started ,打开入门页面,往下拉,找到 Download for Windows 打开 下载页面 ,点击 Please Login To Download ,要注 ...

  3. oc中将CGRect、CGSize、CGPoint等结构体转换为字符串

    CGRect rect = CGRectMake(160, 230, 200, 200); CGPoint point = CGPointMake(20, 20); CGSize size =  CG ...

  4. 自定义AlertView的方法和改变Alert的弹出位置以及其宽度

    此方法在IOS7中不适合 一.自定义AlertView 1.首先新建一个OC类继承与AlertView. 2.然后再.m中添加方法 - (void)layoutSubviews 可以再这个方法里边改变 ...

  5. iOS项目工程及目录结构

    做过一些iOS的项目,不同项目的沉淀没有积累到一起,目录的管理都在后期随着人员的增加越来越混乱,因此在这里做一些梳理,希望达到两个目的. 一套相对通用的目录结构,作为后续项目的模版. 积累相应的基础库 ...

  6. Linux基础学习-NFS网络文件系统实时文件共享

    NFS网络文件系统 如果大家觉得Samba服务程序的配置太麻烦了,那么你共享文件的主机都是Linux系统,那么推荐大家在客户端部署nfs服务来共享文件.nfs(网络文件系统)服务可以将远程Linux系 ...

  7. Eclipse将java项目导出可执行的jar文件

    1.在java项目上右键,点击“Export”,会弹出一个选择导出的文件类型 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址:https://www.cnblogs.com/poter ...

  8. Python基础——文件操作

    写文件 writefile %%writefile ./data/testFile.txt hello python jin tian tian qi bu cuo open覆盖 txt=open(' ...

  9. Python语言的简介

    ___________________________________________________________我是一条分割线__________________________________ ...

  10. pandas-Notes1

    #coding = utf-8 import pandas as pd import numpy as np import matplotlib as plt # series, like vecto ...