Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4569 | Accepted: 1195 |
Description
Input
Each test case contains two lines.
The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].
Output
Sample Input
1 0.5 2 2 0.5 2 4
Sample Output
0.5000000 0.2500000
題解詳見:http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html
AC代碼:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int INF = 1e9;
const int maxn = 1000005;
const int mod = 100000000;
int n;
double p;
int mine[15];
struct Mat{
double mat[2][2];
void zero(){
memset(mat, 0, sizeof(mat));
}
void unit(){
zero();
for(int i = 0; i < 2; i++) mat[i][i] = 1;
}
}A, T;
Mat operator * (const Mat &a, const Mat & b){
Mat tmp;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
{
double sum = 0;
for(int k = 0; k < 2; k++)
sum += a.mat[i][k] * b.mat[k][j];
tmp.mat[i][j] = sum;
}
return tmp;
}
Mat operator ^ (Mat x, int m){
Mat tmp;
tmp.unit();
while(m)
{
if(m & 1) tmp = tmp * x;
x = x * x;
m >>= 1;
}
return tmp;
}
void init(){
T.zero();
T.mat[0][0] = p, T.mat[0][1] = 1 - p, T.mat[1][0] = 1;
}
int main()
{
while(~scanf("%d%lf", &n, &p))
{
init();
for(int i = 0; i < n; i++)
scanf("%d", &mine[i]);
sort(mine, mine + n);
double ans = 1;
Mat tmp = T ^ (mine[0] - 1);
ans *= (1 - tmp.mat[0][0]);
for(int i = 1; i < n; i++)
{
if(mine[i] == mine[i - 1]) continue;
tmp = T ^ (mine[i] - mine[i - 1] - 1);
ans *= (1 - tmp.mat[0][0]);
}
printf("%.7f\n", ans);
}
return 0;
}