http://poj.org/problem?id=3744
Commit two,, recursive, matrix power what will, but I push to be different from others,,, should be on the matrix to understand the problem,, and then look at
#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include < iostream> #include <iomanip> #include <cmath> #include <map> #include <set> #include <
Queue> using namespace std; #define LS (RT) rt*2 #define RS (RT) rt*2+1 #define LL long #define ULL unsigned long long #define REP (i,s,e) for (int i =s;i<e;i++) #define REPE (i,s,e) for (int i=s;i<=e;i++) #define CL (A, B) memset (A,b,sizeof (a)) #define in (s) freopen (
S, "R", stdin) #define OUT (s) freopen (S, "w", stdout) const int maxn=3;
Double MTR[MAXN][MAXN];
Double ANSM[MAXN][MAXN];
int sz=2;
void mulmtr (double x[maxn][maxn], double Y[MAXN][MAXN]) {double TMP[MAXN][MAXN];
for (int i=0;i<sz;i++) for (int j=0;j<sz;j++) {tmp[i][j]=0;
for (int k=0;k<sz;k++) TMP[I][J]+=X[I][K]*Y[K][J];
} for (int i=0;i<sz;i++) for (int j=0;j<sz;j++) Y[I][J]=TMP[I][J];
}
void Mtrmi (double mtr[maxn][maxn],int n) {for (Int. i=0;i<sz;i++) for (int j=0;j<sz;j++) {
if (i = = j) ansm[i][j]=1;
else ansm[i][j]=0;
} while (n) {if (n&1) {mulmtr (MTR,ANSM);
} mulmtr (MTR,MTR);
n/=2;
}} int bomb[maxn*10];
int main () {//in ("poj3744.txt");
int n;
int last;
Double p;
Double ans=1.0;
int POS;
while (~SCANF ("%d%lf", &n,&p)) {ans=1.0;
Last=1;
for (int i=0;i<n;i++) scanf ("%d", &bomb[i]);
Sort (bomb,bomb+n);
for (int i=0;i<n;i++) {pos=bomb[i]-last;
/*if (pos = = 0) {last=pos+1;continue;}
if (pos = = 1) {ans*= (1-p); last=pos+1;continue;} if (pos = = 2) {ans*=p;last=pos+1;continue;}
*///pos-=2;
Mtr[0][0]=p;
Mtr[0][1]=1.0-p;
mtr[1][0]=1.0;
mtr[1][1]=0.0; Mtrmi (Mtr,pos);
ANS*=1.0-ANSM[0][0];
last=bomb[i]+1;
} cout << Fixed << setprecision (7) << ans << Endl;
} return 0;
}