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. 关键字final

    final数据 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象.再次赋值将引起编译报错. 当f ...

  2. Linux - 后台运行 ctrl + z , jobs , bg , fg

    一.& 最经常被用到 这个用在一个命令的最后,可以把这个命令放到后台执行 二.ctrl + z 可以将一个正在前台执行的命令放到后台,并且暂停三.jobs查看当前有多少在后台运行的命令四.fg ...

  3. manjaro kde tim QQ

    deepin-wine-tim

  4. OpenCV中的图像形态学转换

    两个基本的形态学操作是腐蚀和膨胀.他们的变化构成了开运算,闭运算,梯度等.下面以这张图为例 1.腐蚀 这个操作会把前景物体的边界腐蚀掉. import cv2 import numpy as np i ...

  5. Python9-网络编程2-day31

    基于UDP协议的socket #server import socket sk = socket.socket(type=socket.SOCK_DGRAM) sk.bind(('127.0.0.1' ...

  6. Django之FileField字段

    头像上传 在头像上传的时候,属于文件类型 首先视图函数获取的时候,request.FILES.get('文件名变量') avatar_obj = request.FILES.get('avatar') ...

  7. Leetcode(204) Count Primes

    题目 Description: Count the number of prime numbers less than a non-negative number, n. Credits: Speci ...

  8. stm32之ADC应用实例(单通道、多通道、基于DMA)

    文本仅做记录.. 硬件:STM32F103VCT6 开发工具:Keil uVision4 下载调试工具:ARM仿真器 网上资料很多,这里做一个详细的整合.(也不是很详细,但很通俗).  所用的芯片内嵌 ...

  9. 水题:CF16C-Monitor

    Monitor 题目描述 Reca company makes monitors, the most popular of their models is AB999 with the screen ...

  10. PAT Basic 1075

    1075 链表元素分类 给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而 [0, K] 区间内的元素都排在大于 K 的元素前面.但每一类内部元素的顺序是不能 ...