POJ3744(概率dp+矩阵快速幂)
来源:互联网 发布:软件微电子 综合信息 编辑:程序博客网 时间:2024/05/16 11:06
老实说,经过一番WA哈哈后,独立A了一道题,这种写题解的冲动真是忍不住,好久没独立A了,呜呜~~~不废话了,贴码————
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <vector>#include <set>#include <map>using namespace std;typedef long long ll;const int N = 5;//→_→其实好像可以不用5这么多int a[20];//存放不能走的坐标map<int, int>vis;//判断是否能走,能走就是0,不能就是1//很常见的矩阵快速幂重载模板struct node {double jz[N][N];node operator * (const node& m)const{node ans;for(int i = 0; i < N; i++) {for(int j = 0; j < N; j++) {ans.jz[i][j] = 0;for(int k = 0; k < N; k++) {ans.jz[i][j] += jz[i][k] * m.jz[k][j];}}}return ans;}};//第一行x[],其余全是0的矩阵*pow(c,k);快速幂void Pow(double x[], node c, int k){node ans;memset(ans.jz, 0, sizeof(ans.jz));for(int i = 0; i < N; i++)ans.jz[0][i] = x[i];while(k) {if(k % 2)ans = ans * c;c = c * c;k /= 2;}for(int i = 0; i < N; i++)x[i] = ans.jz[0][i];}int main(){int n;double p;node c;memset(c.jz, 0, sizeof(c.jz));//初始化常量矩阵for(int i = 1; i < N; i++)c.jz[i][i - 1] = 1;//左移一行,如:{a,b,c,d,e}->{b,c,d,e,f}while(cin >> n >> p) {a[0] = -20;vis.clear();c.jz[3][4] = 1 - p;c.jz[4][4] = p; //最后一个数,即f = d*(1-p) + e*p;for(int i = 1; i <= n; i++) {scanf("%d", &a[i]);vis[a[i]] = 1;}sort(a + 1, a + n + 1);double f[N] = {0, 0, 0, 0, 1};//末尾1代表安全走到1位置概率为1(不管1能不能走,只是走到),之前以为是0→_→被坑了好久a[n + 1] = a[n] + 1;//最后答案当然是走到a[n+1]Pow(f, c, a[1] + 1);//→_→这个可以自己调,反正我是调到最终f[2]处对应a[1]for(int i = 1; i <= n; i++) {for(int j = 3; j < N; j++) f[j] = !vis[a[i] - 1 + (j - 2)] * f[j - 1] * p + !vis[a[i] - 2 + (j - 2)] * f[j - 2] * (1 - p); /* vis[a[i]-1+(j-2)]判断当前能不能走,能走返回0,随意要取逻辑反,表示要计算,否则=0舍去 因为原来的f[3],f[4]都是在f[2]能走的情况下取的,很明显,此刻f[2]不能取(a[i]对应f[2]),所以更新f[3],f[4] */Pow(f, c, a[i + 1] - a[i]);//调到f[2]处对应a[i+1],所以最终f[2]就是答案}printf("%.7f\n", f[2]);}return 0;}
0 0
- poj3744之矩阵快速幂+概率DP
- POJ3744(概率dp+矩阵快速幂)
- poj3744 概率dp + 矩阵快速幂
- poj3744 Scout YYF I 概率dp+矩阵快速幂
- POJ3744 Scout YYF I ( 矩阵快速幂 + 期望概率DP )
- poj3744(概率dp+快速幂)
- poj3744(概率DP+矩阵优化)
- poj3744 概率dp+矩阵乘法
- poj3744——Scout YYF I(概率DP+矩阵快速幂)
- poj3744(概率DP+矩阵二分)
- poj3744(概率DP)
- poj3744--Scout YYF I(概率dp第五弹:矩阵优化)
- POJ3744 Scout YYF I (概率DP + 矩阵优化)
- 【POJ3744】Scout YYF I-概率DP+矩阵加速优化
- POJ3744:Scout YYF I(概率dp & 矩阵优化)
- poj3744 Scout YYF I 【概率dp+矩阵优化】
- POJ 3744 概率dp+矩阵快速幂
- POJ3744-Scout YYF I--概率DP
- lua学习
- LeetCode Remove Element
- 亚克力
- C#基础 之 初始调试
- 填空
- POJ3744(概率dp+矩阵快速幂)
- 通知栏清理与查询
- solr 简单搭建 数据库数据同步(待续)
- (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
- html相关笔记,我所不知道的,没注意到的,或者称之为常识...
- HDU 1506 Largest Rectangle in a Histogram
- mysql数据库的访问控制
- 推荐无弹窗无广告小说网
- Vector的用法