poj 3744 Scout YYF I (矩阵快速幂+概率dp)

Scout YYF I
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4374 Accepted: 1141

Description

YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road". This is a very long road, on which there are numbers of mines. At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of  p, or jump two step with a probality of 1- p. Here is the task, given the place of each mine, please calculate the probality that YYF can go through the "mine road" safely.

Input

The input contains many test cases ended with  EOF.
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

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.

Sample Input

1 0.5
2
2 0.5
2 4

Sample Output

0.5000000
0.2500000

Source



思路来源于:博博(题解推荐)

题意:

你在一条布满地雷的道路上,开始在坐标1。每次有概率P向前走一步,有概率1-P向前走两步。道中路某几个点上会有地雷,问你安全通过的概率。地雷数N<=10,坐标范围在100000000内。


思路:

第一次接触矩阵快速幂,其实就是二进制的思想吧,留个纪念。

dp[i]=dp[i-1]*p+dp[i-2]*(1-p);

构造矩阵

| P ,1-P |
| 1 , 0    |

那么有

|dp[n]   |           |p      1-p|^n-1     |dp[1]|

                  =                           *

|dp[n-1]|          |1          0|            |dp[0]|

然后将每个a[i]+1到a[i+1]看做一段单独处理就行。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define MAXN 100005
#define OO (1<<31)-1
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,tot,flag;
double p,ans;
int a[15];

struct Matrix
{
    int row,col;//行列
    double v[3][3];
    Matrix operator*(Matrix &tt)//矩阵相乘。前面矩阵的列必须和后面矩阵的行相同
    {
        int i,j,k;
        Matrix temp;
        temp.row=row;
        temp.col=tt.col;
        for(i=0; i<row; i++)
            for(j=0; j<tt.col; j++)
            {
                temp.v[i][j]=0;
                for(k=0; k<col; k++)
                    temp.v[i][j]+=v[i][k]*tt.v[k][j];
            }
        return temp;
    }
};
Matrix pow_mod(Matrix x,int i)
{
    Matrix tmp;
    tmp.row=tmp.col=2;
    tmp.v[0][0]=tmp.v[1][1]=1;
    tmp.v[0][1]=tmp.v[1][0]=0;
    while(i)
    {
        if(i&1) tmp=tmp*x;
        x=x*x;
        i>>=1;
    }
    return tmp;
}
int main()
{
    int i,j,t;
    while(~scanf("%d%lf",&n,&p))
    {
        a[0]=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n+1);
        if(a[1]==1)
        {
            ans=0;
            printf("%.7f\n",ans);
            continue ;
        }
        ans=1;
        for(i=1;i<=n;i++)
        {
            Matrix jj;
            jj.row=jj.col=2;
            jj.v[0][0]=p; jj.v[0][1]=1-p;
            jj.v[1][0]=1; jj.v[1][1]=0;
            t=a[i]-a[i-1]-1;
            Matrix yy=pow_mod(jj,t);
            ans*=(1-yy.v[0][0]);
        }
        printf("%.7f\n",ans);
    }
    return 0;
}
/*
2 0.5
2 2
2 0.5
2 4
*/







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java解决POJ3233—矩阵幂序列问题的代码和解释: ```java import java.util.Scanner; public class Main { static int n, k, m; static int[][] A, E; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); k = sc.nextInt(); m = sc.nextInt(); A = new int[n][n]; E = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i][j] = sc.nextInt() % m; E[i][j] = (i == j) ? 1 : 0; } } int[][] res = matrixPow(A, k); int[][] ans = matrixAdd(res, E); printMatrix(ans); } // 矩阵乘法 public static int[][] matrixMul(int[][] a, int[][] b) { int[][] c = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % m; } } } return c; } // 矩阵快速幂 public static int[][] matrixPow(int[][] a, int b) { int[][] res = E; while (b > 0) { if ((b & 1) == 1) { res = matrixMul(res, a); } a = matrixMul(a, a); b >>= 1; } return res; } // 矩阵加法 public static int[][] matrixAdd(int[][] a, int[][] b) { int[][] c = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c[i][j] = (a[i][j] + b[i][j]) % m; } } return c; } // 输出矩阵 public static void printMatrix(int[][] a) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } } ``` 解释: 1. 首先读入输入的n、k、m和矩阵A,同时初始化单位矩阵E。 2. 然后调用matrixPow函数求出A的k次幂矩阵res。 3. 最后将res和E相加得到结果ans,并输出。 4. matrixMul函数实现矩阵乘法,matrixPow函数实现矩阵快速幂,matrixAdd函数实现矩阵加法,printMatrix函数实现输出矩阵。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值