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

本文介绍了一种使用矩阵快速幂解决安全通过布满地雷的道路问题的方法。文章详细阐述了如何构建矩阵并利用二进制思想进行快速幂运算,以此来高效计算安全通过道路的概率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
*/







### 关于POJ 1995问题的快速幂C++实现 对于POJ 1995问题,其核心在于通过矩阵快速幂算法高效解决大规模数据下的指数运算。以下是基于引用材料中的相关内容构建的一个完整的解决方案。 #### 矩阵快速幂的核心逻辑 矩阵快速幂是一种高效的计算方式,在处理线性递推关系时尤为有效。例如斐波那契数列可以通过构造特定的转移矩阵来加速计算[^4]。具体来说,给定一个初始状态向量和一个转移矩阵,经过若干次幂运算后可获得目标状态。 以下是一个通用的矩阵快速幂模板: ```cpp #include <iostream> using namespace std; const int N = 2; // 定义矩阵大小 struct Matrix { long long m[N][N]; }; // 矩阵乘法函数 Matrix multiply(const Matrix& a, const Matrix& b) { Matrix c; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { c.m[i][j] = 0; for (int k = 0; k < N; ++k) { c.m[i][j] += a.m[i][k] * b.m[k][j]; c.m[i][j] %= 10000; // 取模操作 } } } return c; } // 快速幂函数 Matrix fastPower(Matrix base, long long exp) { Matrix result; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { result.m[i][j] = (i == j); } } while (exp > 0) { if (exp % 2 == 1) { result = multiply(result, base); } base = multiply(base, base); exp /= 2; } return result; } int main() { long long n; cin >> n; // 初始化转移矩阵 Matrix trans; trans.m[0][0] = 0; trans.m[0][1] = 1; trans.m[1][0] = 1; trans.m[1][1] = 1; // 计算结果矩阵 if (n == 0 || n == 1) { cout << 1 << endl; } else { Matrix res = fastPower(trans, n - 1); // 初始状态向量 long long fib_prev = 1; long long fib_curr = 1; // 输出结果 cout << (res.m[0][0] * fib_prev + res.m[0][1] * fib_curr) % 10000 << endl; } return 0; } ``` 此代码实现了针对斐波那契数列的大规模项求解功能,并采用了取模`%10000`的操作以满足题目需求。其中的关键部分包括矩阵乘法、快速幂以及状态转移的设计[^3]。 #### 特殊注意点 在实际提交过程中需要注意以下几个方面: - **大数组定义**:如果涉及更大的矩阵或者更复杂的动态规划表,则需特别留意内存分配的位置及其范围限制[^2]。 - **时间复杂度控制**:尽管快速幂本身具有较低的时间复杂度O(log n),但在极端情况下仍需验证是否存在进一步优化空间。 - **边界条件处理**:如输入为较小数值时直接返回已知答案而非进入循环计算流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值