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. Bootstrap历练实例:导航内的下拉菜单

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. Windows系统安装docker

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

  3. redis基础知识学习

    数据结构:1.String 添加: set key value get key getset key value (先get再set) incr key (key对应value原子性递增1) decr ...

  4. Js 数组去重的几种方法总结

           去重是开发中经常会碰到的一一个热点问题,不过目前项目中碰到的情况都是后台接口使用SQL去重,简单高效,基本不会让前端处理去重.那么前端处理去重会出现什么情况呢?假如每页显示10条不同的数 ...

  5. 【Kafka】搭建和测试等问题

    1.安装启动kafka #跳转到下载目录cd /opt/setup # 下载安装包 wget http://mirror.bit.edu.cn/apache/kafka/0.10.2.0/kafka_ ...

  6. python3 简单服务器监控,自动发送邮件

    import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimpo ...

  7. PAT Basic 1081

    1081 检查密码 本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能.该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母.数字和小数点 .,还必须既有字母也有数字. 输 ...

  8. Linux学习-系统基本设定

    网络设定 (手动设定与 DHCP 自动取得) 网络其实是又可爱又麻烦的玩意儿,如果你是网络管理员,那么你必须要了解局域网络内的 IP, gateway, netmask 等参数,如果还想要连上 Int ...

  9. 像玩魔兽一样编程——谈VS2010键盘流

    早年在学校里的时候,经常玩War3,那时候很痴迷,也经常看sky.moon的一些第一视角,有的时候也会模仿模仿...好吧,往事不堪回首,现在工作了,谈一谈.Net程序猿使用VS的键盘流,如果你不知道s ...

  10. 安装liteIDE on mac

    download and install: http://sourceforge.net/projects/liteide/files/ 解决不能编译,没有自动完成的问题: http://stacko ...