【题目链接】
http://poj.org/problem?id=3744
【算法】
概率DP + 矩阵乘法
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; int i,n; double p,g,ans; double x[20]; struct Matrix { double mat[3][3]; } m; inline Matrix multipy(Matrix &a,Matrix b) { int i,j,k; Matrix res; memset(res.mat,0,sizeof(res.mat)); for (i = 1; i <= 2; i++) { for (j = 1; j <= 2; j++) { for (k = 1; k <= 2; k++) { res.mat[i][j] += a.mat[i][k] * b.mat[k][j]; } } } return res; } inline Matrix power(Matrix a,int n) { int i,j; Matrix res,b = a; for (i = 1; i <= 2; i++) { for (j = 1; j <= 2; j++) { res.mat[i][j] = (i == j); } } while (n) { if (n & 1) res = multipy(res,b); b = multipy(b,b); n >>= 1; } return res; } int main() { while (scanf("%d%lf",&n,&p) != EOF) { for (i = 1; i <= n; i++) scanf("%lf",&x[i]); sort(x+1,x+n+1); m.mat[1][1] = p; m.mat[1][2] = 1 - p; m.mat[2][1] = 1; m.mat[2][2] = 0; ans = 1.0; x[0] = 0; for (i = 1; i <= n; i++) { m = power(m,x[i]-x[i-1]-1); g = m.mat[1][1]; ans *= (1 - g); m.mat[1][1] = p; m.mat[1][2] = 1 - p; m.mat[2][1] = 1; m.mat[2][2] = 0; } printf("%.7lf ",ans); } return 0; }