Consider a system which is described at any time as being in one of a set of N distinct states, 1,2,3,...,N. We denote the time instants associated with state changes as t=1,2,..., and the actual state at time t as aij=p=[si=j ∣ si−1=i],1≤i,j≤N. For the special case of a discrete, first order, Markovchain, the probabilistic description for the current state (at time t) and the predecessor state is st. Furthermore we only consider those processes being independent of time, thereby leading to the set of state transition probability aij of the form: with the properties aij≥0 and ∑i=1NAij=1. The stochastic process can be called an observable Markovmodel. Now, let us consider the problem of a simple 4-state Markov model of weather. We assume that once a day (e.g., at noon), the weather is observed as being one of the following:
State 1: snow
State 2: rain
State 3: cloudy
State 4: sunny
The matrix A of state transition probabilities is:
A={aij}=⎩⎪⎪⎨⎪⎪⎧a11a21a31a41a12a22a32a42a13a23a33a43a14a24a34a44⎭⎪⎪⎬⎪⎪⎫
Given the model, several interestingquestions about weather patterns over time can be asked (and answered). We canask the question: what is the probability (according to the given model) thatthe weather for the next k days willbe? Another interesting question we can ask: given that the model is in a knownstate, what is the expected number of consecutive days to stay in that state?Let us define the observation sequence Oas O={s1,s2,s3,...,sk}, and the probability of the observation sequence Ogiven the model is defined as p(O∣model). Also, let the expected number of consecutive days to stayin state i be Ei. Assume that the initial state probabilities p[s1=i]=1,1≤i≤N. Bothp(O∣model)and Ei are real numbers.
Input Format
Line 1~4 for the state transition probabilities. Line 5 for the observation sequence O1, and line 6 for the observation sequence O2. Line 7and line 8 for the states of interest to find the expected number of consecutive days to stay in these states.
Line 1: a11 a12 a13 a14
Line 2: a21 a22 a23 a34
Line 3: a31 a32 a33 a34
Line 4: a41 a42 a43 a44
Line 5: s1 s2 s3 ... sk
Line 6: s1 s2 s3 ... sl
Line 7: i
Line 8: j
Output Format
Line 1 and line 2 are used to show the probabilities of the observation sequences O1and O2 respectively. Line 3 and line 4 are for the expected number of consecutive days to stay in states i and j respectively.
Line 1: p[O1∣model]
Line 2: p[O2∣model]
Line 3: Ei
Line 4: Ej
Please be reminded that the floating number should accurate to 10−8.
樣例輸入
0.4 0.3 0.2 0.1
0.3 0.3 0.3 0.1
0.1 0.1 0.6 0.2
0.1 0.2 0.2 0.5
4 4 3 2 2 1 1 3 3
2 1 1 1 3 3 4
3
4
樣例輸出
0.00004320
0.00115200
2.50000000
2.00000000
題目來源
題意:先給你4 * 4 的矩陣,表示天氣 i 到天氣 j 轉變的概率,然後輸入 k 個數字,以 ‘\n’ 表示輸入結束,這 k 個數字的意思以4 4 3 2 2 1 1 3 3
這組數字舉例,其實就是天氣從 4 轉變爲 4,然後從 4 轉變爲 3,然後從 3 轉變爲 2,然後從 2 轉變爲 2,然後從 2 轉變爲 1,然後從 1 轉變爲 1,然後從1 轉變爲 3,然後從3 轉變爲 3的概率是多少。
這樣的輸入數據有兩組
然後輸入兩個數字 i 和 j ,這兩個數字都表示天氣的狀況爲 i 和 j 一直不變,不管天數增加多少,然後要你求一直不變的數學期望是多少。
附代碼:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
///o(っ。Д。)っ AC萬歲!!!!!!!!!!!!!!
const int maxn = 10;
double maps[maxn][maxn] = {};
int main()
{
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= 4; j++)
{
scanf("%lf", &maps[i][j]);
}
}
double pp = 1.0;
int d1, d2;
char ch;
scanf("%d", &d1);
while(scanf("%d%c", &d2, &ch) != EOF)
{
pp *= maps[d1][d2];
d1 = d2;
if(ch == '\n') break;
}
printf("%.8lf\n", pp);
pp = 1.0;
scanf("%d", &d1);
while(scanf("%d%c", &d2, &ch) != EOF)
{
pp *= maps[d1][d2];
d1 = d2;
if(ch == '\n') break;
}
printf("%.8lf\n", pp);
scanf("%d", &d1);
pp = maps[d1][d1];
double sum = 0.0;
while(pp >= 0.000000001)
{
sum += pp;
pp *= maps[d1][d1];
}
printf("%.8lf\n", sum + 1.0);
sum = 0.0;
scanf("%d", &d2);
pp = maps[d2][d2];
while(pp >= 0.000000001)
{
sum += pp;
pp *= maps[d2][d2];
}
printf("%.8lf\n", sum + 1.0);
return 0;
}
/*
*/