#include<stdio.h>
#include<math.h>
#include <stdlib.h>
float A[3][3]={0.5,0.2,0.3,
0.3,0.5,0.2,
0.2,0.3,0.5};
float B[3][2]={0.5,0.5,
0.4,0.6,
0.7,0.3};
int o[4]={0,1,0,1},t=4;
float pi[3]={0.2,0.4,0.4};
float a[4][4]={0};
int main()
{
int i,j,z;
float q=0;
for(i=0;i<3;i++)
a[0][i]=pi[i]*B[i][0];
for(j=1;j<t;j++)
{
for(z=0;z<3;z++)
{
q=0;
for(i=0;i<3;i++)
q=a[j-1][i]*A[i][z]+q;
a[j][z]=q*B[z][o[j]];
}
}
printf("%f",a[t-1][0]+a[t-1][1]+a[t-1][2]);
}