hdu 4418 高斯消元求期望

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

2012 ACM/ICPC Asia Regional Hangzhou Online

题意:一个人在数轴上来回走,以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)

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <queue>
  6 using namespace std;
  7
  8 const int maxn=205;
  9 const double eps=1e-8;
 10 int map[maxn],flag[maxn];
 11 double p[maxn],A[maxn][maxn];
 12 int cnt,n,m,st,ed,d;
 13 int dcmp(double x)
 14 {
 15     if(fabs(x)<eps) return 0;
 16     else if(x-0>eps) return 1;
 17     return -1;
 18 }
 19 void swap(double &a,double &b){double t=a;a=b;b=t;}
 20
 21 bool bfs()
 22 {
 23     memset(flag,-1,sizeof(flag));
 24     queue<int>Q;
 25     cnt=0;flag[st]=cnt++;
 26     Q.push(st);
 27     bool ret=false;
 28     while(!Q.empty())
 29     {
 30         int u=Q.front();Q.pop();
 31         for(int i=1;i<=m;i++)
 32         {
 33             int v=(u+i)%(2*n-2);
 34             if(dcmp(p[i])==0) continue;
 35             if(flag[v]!=-1) continue;
 36             flag[v]=cnt++;
 37             if(map[v]==ed) ret=true;
 38             Q.push(v);
 39         }
 40     }
 41     return ret;
 42 }
 43
 44 void bulidmatrix()
 45 {
 46     memset(A,0,sizeof(A));
 47     for(int i=0;i<2*n-2;i++)
 48     {
 49         if(flag[i]==-1) continue;
 50         int u=flag[i];A[u][u]=1;
 51         if(map[i]==ed){A[u][cnt]=0;continue;}
 52         for(int j=1;j<=m;j++)
 53         {
 54             int v=(i+j)%(2*n-2);
 55             if(flag[v]==-1) continue;
 56             v=flag[v];
 57             A[u][v]-=p[j];A[u][cnt]+=p[j]*j;
 58         }
 59     }
 60 }
 61
 62 void gauss(int n)
 63 {
 64     int i,j,k,r;
 65     for(i=0;i<n;i++)
 66     {
 67         r=i;
 68         for(j=i+1;j<n;j++)
 69             if(fabs(A[j][i])>fabs(A[r][i])) r=j;
 70         if(dcmp(A[r][i])==0) continue;
 71         if(r!=i) for(j=0;j<=n;j++) swap(A[r][j],A[i][j]);
 72         for(k=0;k<n;k++) if(k!=i)
 73             for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
 74     }
 75 }
 76
 77 int main()
 78 {
 79     int i,j,t;
 80     scanf("%d",&t);
 81     while(t--)
 82     {
 83         scanf("%d%d%d%d%d",&n,&m,&ed,&st,&d);
 84         for(i=1;i<=m;i++){ scanf("%lf",p+i);p[i]/=100;}
 85         if(st==ed){ printf("0.00\n");continue;}
 86         for(i=0;i<n;i++) map[i]=i;
 87         for(i=n,j=n-2;i<2*n-2;i++,j--) map[i]=j;
 88         if(d==1) st=2*n-2-st;
 89         if(!bfs()){ printf("Impossible !\n");continue;}
 90         bulidmatrix();gauss(cnt);
 91         for(i=cnt-1;i>=0;i--)
 92         {
 93             for(j=i+1;j<cnt;j++)
 94                 A[i][cnt]-=A[j][cnt]*A[i][j];
 95             A[i][cnt]/=A[i][i];
 96         }
 97         printf("%.2lf\n",A[0][cnt]);
 98     }
 99     return 0;
100 }

hdu 4418 高斯消元求期望

时间: 2024-10-13 08:00:29

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

hdu 2262 高斯消元求期望

Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1070    Accepted Submission(s): 298 Problem Description After a long drastic struggle with himself, LL decide to go for some

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

Crazy Typewriter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 391    Accepted Submission(s): 109 Problem Description There was a crazy typewriter before. When the writer is not very sober, it

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

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 654    Accepted Submission(s): 415 Special Judge Problem Description A little gir

hdu 4870 Rating(高斯消元求期望)

http://acm.hdu.edu.cn/showproblem.php?pid=4870 题意:有两个号,初始分数都是0,每次选一个分数较小的打比赛,如果分数一样任选一个,有p的概率涨50分,最高为1000分,有1-p的概率跌100分,最低为0分.问有一个号涨到1000需要打比赛的次数的期望. 令(x, y)表示高分为x,低分为y的状态(x >= y),E(x, y)表示从(x, y)到达(1000, ?)的比赛场数期望.容易得到E(x, y) = P * E(x1, y1) + (1 - 

[ACM] hdu 2262 Where is the canteen (高斯消元求期望)

Where is the canteen Problem Description After a long drastic struggle with himself, LL decide to go for some snack at last. But when steping out of the dormitory, he found a serious problem : he can't remember where is the canteen... Even worse is t

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

题目大意: 一个人在n长的路径上走到底再往回,走i步停下来的概率为Pi , 求从起点开始到自己所希望的终点所走步数的数学期望 因为每个位置都跟后m个位置的数学期望有关 E[i] = sigma((E[i+j]+j)*P[j]) 我们需要将模型转化一下,本来路径为012345这样,因为来回走,我们多定义n-2个点就是 0123454321然后利用取模就可以不断找到下一组相关的m个点 列出多元方程组,利用高斯消元解决问题 1 #include <cstdio> 2 #include <cst

uva 10828 高斯消元求数学期望

Back to Kernighan-RitchieInput: Standard Input Output: Standard Output You must have heard the name of Kernighan and Ritchie, the authors of The C Programming Language. While coding in C, we use different control statements and loops, such as, if-the

hdu 1071 The area 高斯消元求二次函数+辛普森积分

构造系数矩阵,高斯消元求解二次函数,然后两点式求直线函数,带入辛普森积分法无脑AC... #include<cstdio> #include<queue> #include<algorithm> #include<cstring> #include<vector> #include<cmath> using namespace std; struct node { double x,y; }p[4]; double g[10][10]

hdu 3915 高斯消元

Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 724    Accepted Submission(s): 285 Problem Description Mr.Frost is a child who is too simple, sometimes naive, always plays some simple but i