标签:des style c class blog code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4452 | Accepted: 1159 |
Description
Input
Output
Sample Input
1 0.5 2 2 0.5 2 4
Sample Output
0.5000000 0.2500000
Source
#include <stdio.h> #include <algorithm> #define REP(i, n) for(int i = 0; i < n; ++i) #define FF(i, n) for(int i = 1; i <= n; ++i) using namespace std; const int O = 2; int N; struct Matrix{ typedef double type; type mat[O][O]; Matrix operator * (const Matrix &t) const{ Matrix tmp; REP(i, N) REP(j, N){ tmp.mat[i][j] = 0; REP(k, N){ tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k] * t.mat[k][j]); } } return tmp; } Matrix operator + (const Matrix &t) const{ Matrix tmp; REP(i, N) REP(j, N){ tmp.mat[i][j] = (mat[i][j] + t.mat[i][j]); } return tmp; } Matrix operator - (const Matrix &t) const{ Matrix tmp; REP(i, N) REP(j, N){ tmp.mat[i][j] = (mat[i][j] - t.mat[i][j]); } return tmp; } }; Matrix E, A; Matrix pow(Matrix a, int k){ Matrix res = E, tmp = a; while(k){ if(k & 1) res = res * tmp; tmp = tmp * tmp; k >>= 1; } return res; } int work(){ int n, mine[11], flag; double p, ans; N = O; mine[0] = 0; REP(i, N) REP(j, N) E.mat[i][j] = (i == j); while(~scanf("%d%lf", &n, &p)){ FF(i, n) scanf("%d", &mine[i]); sort(mine + 1, mine + n + 1); flag = 0; FF(i, n){ if(mine[i] - mine[i - 1] == 1) flag = 1; } if(mine[0] == 1 || flag){ printf("%.7f\n", 0); continue; } A.mat[0][0] = p; A.mat[0][1] = 1; A.mat[1][0] = 1 - p; A.mat[1][1] = 0; ans = 1; FF(i, n){ if(mine[i] == mine[i - 1]) continue; ans *= 1 - pow(A, mine[i] - mine[i - 1] - 1).mat[0][0]; } printf("%.7f\n", ans); } return 0; } int main(){ return work(); }
POJ 3744 Scout YYF I 矩阵快速幂,布布扣,bubuko.com
标签:des style c class blog code
原文地址:http://www.cnblogs.com/ac-luna/p/3754864.html