标签:dp
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5381 | Accepted: 1475 |
Description
Input
Output
Sample Input
1 0.5 2 2 0.5 2 4
Sample Output
0.5000000 0.2500000
Source
/************************************************************************* > File Name: POJ3744.cpp > Author: ALex > Mail: 405045132@qq.com > Created Time: 2014年12月24日 星期三 21时20分51秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int pos[15]; struct martix { double mat[2][2]; }; martix multiply(martix a, martix b) { martix c; c.mat[0][0] = c.mat[0][1] = c.mat[1][0] = c.mat[1][1] = 0; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { c.mat[i][j] += a.mat[i][k] * b.mat[k][j]; } } } return c; } martix fast_pow(martix a, int b) { martix c; c.mat[0][0] = 1; c.mat[0][1] = c.mat[1][0] = 0; c.mat[1][1] = 1; while (b) { if (b & 1) { c = multiply(c, a); } a = multiply(a, a); b >>= 1; } return c; } int main() { int n; double p; while (~scanf("%d%lf", &n, &p)) { for (int i = 1; i <= n; ++i) { scanf("%d", &pos[i]); } double ans = 1; sort (pos + 1, pos + n + 1); martix x; x.mat[0][0] = p; x.mat[0][1] = 1 - p; x.mat[1][0] = 1; x.mat[1][1] = 0; martix tmp; tmp = fast_pow(x, pos[1] - 1); ans *= (1 - tmp.mat[0][0]); for (int i = 2; i <= n; ++i) { if (pos[i] == pos[i - 1]) { continue; } tmp = fast_pow(x, pos[i] - pos[i - 1] - 1); ans *= (1 - tmp.mat[0][0]); } printf("%.7f\n", ans); } return 0; }
标签:dp
原文地址:http://blog.csdn.net/guard_mine/article/details/42147105