2015
07-16

Time travel

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.

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.

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.

2
4 2 0 1 0
50 50
4 1 0 2 1
100

8.14
2.00

by—cxlove

E[i]表示在i点的时候到达终点的期望步数，则E[i]=E[i+1]*p1+E[i+2]*p2+……E[i+m]*pm+1。

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#define inf 1<<28
#define M 6000005
#define N 205
#define maxn 300005
#define eps 1e-8
#define zero(a) fabs(a)<eps
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson step<<1
#define rson step<<1|1
using namespace std;
int n,m,y,x,s,d,e;
double p[N],a[N][N];
int cnt,id[N],g[N];
void debug(int n){
for(int i=0;i<n;i++){
for(int j=0;j<=n;j++)
printf("%.2f ",a[i][j]);
printf("\n");
}
}
bool bfs(){
queue<int>que;
que.push(s);
cnt=0;mem(id,-1);
id[s]=cnt++;
bool flag=false;
while(!que.empty()){
int u=que.front();que.pop();
for(int i=1;i<=m;i++){
int v=(u+i)%(2*n-2);
if(zero(p[i])) continue;
if(id[v]!=-1) continue;
id[v]=cnt++;
if(g[v]==y) {flag=true;}
que.push(v);
}
}
return flag;
}
void Bulid(){
mem(a,0);
for(int i=0;i<(2*n-2);i++){
if(id[i]==-1) continue;
int u=id[i];
a[u][u]=1;
if(g[i]==y){a[u][cnt]=0;continue;}
for(int j=1;j<=m;j++){
int v=(i+j)%(2*n-2);
if(id[v]==-1) continue;
v=id[v];
a[u][v]-=p[j];
a[u][cnt]+=p[j]*j;
}
}
}
bool gauss(int n){
int i,j;
for(i=0,j=0;i<n&&j<n;j++){
int k;
for(k=i;k<n;k++)
if(!zero(a[k][j]))
break;
if(k<n){
if(i!=k)
for(int r=j;r<=n;r++) swap(a[i][r],a[k][r]);
double tt=1/a[i][j];
for(int r=j;r<=n;r++)
a[i][r]*=tt;
for(int r=0;r<n;r++)
if(r!=i){
for(int t=n;t>=j;t--)
a[r][t]-=a[r][j]*a[i][t];
}
i++;
}
}
for(int r=i;r<n;r++)
if(!zero(a[r][n]))
return false;
return true;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d%d",&n,&m,&y,&x,&d);
for(int i=1;i<=m;i++){scanf("%lf",&p[i]);p[i]/=100.0;}
if(x==y){printf("0.00\n");continue;}
if(d==0)s=x;
else if(d==1) s=n+(n-2-x);
else s=x;
for(int i=0;i<n;i++) g[i]=i;
for(int i=n,j=n-2;j>=1;j--,i++) g[i]=j;
if(!bfs()){puts("Impossible !");continue;}
Bulid();
if(!gauss(cnt)) puts("Impossible !");
else{
for(int i=0;i<cnt;i++)
if(zero(a[i][0]-1)){
double ans=a[i][cnt];
if(zero(ans)) printf("0.00\n");
else  printf("%.2f\n",ans);
break;
}
}
}
return 0;
}

1. 神奇的发现本文链接居然是http://wange.im/fuck-the-related-departments.html 与君心有戚戚焉哥在考虑是不是也在博客发个fuck-the-related-departments-too.html ~~