http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2623

The number of steps

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms …). Now she stands at the top point(the first layer), and the KEY of this maze is in the lowest layer’s leftmost room. Known that each room can only access to its left room and lower left and lower right rooms .If a room doesn’t have its left room, the probability of going to the lower left room and lower right room are a and b (a + b = 1 ). If a room only has it’s left room, the probability of going to the room is 1. If a room has its lower left, lower right rooms and its left room, the probability of going to each room are c, d, e (c + d + e = 1). Now , Mary wants to know how many steps she needs to reach the KEY. Dear friend, can you tell Mary the expected number of steps required to reach the KEY?

 

输入

There are no more than 70 test cases.

 
In each case , first Input a positive integer n(0<n<45),
The input is terminated with 0. This test case is not to be processed.

输出

Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.

示例输入

3
0.3 0.7
0.1 0.3 0.6
0

示例输出

3.41

提示

 

来源

2013年山东省第四届ACM大学生程序设计竞赛

示例程序

分析:

第一行有一个位置,第二行两个,第三行三个......第n行n个。此时你在最左上角的位置,如果你左面没有位置,只能往左下和右下走,概率(a,b)。否则可以往左,左下和右下三个方向走,,概率(c,d,e)。让你求到达最左下角的期望。

先科普一下数学期望吧:

首先,来看下期望有啥基本的公式。对离散型随机变量x,其概率为p,有

对随机变量A、B,有 

第二个式子表明了期望有线性的性质,简单理解就是期望之间可根据关系,简单运算(不严谨的理解)。 这就为我们解决一个期望问题,不断转化为解决另外的期望问题,最终转化到一个已知的期望上。

举一个求期望最简单的例子,见下图:

假设有个人在 1号节点处,每一分钟他会缘着边随机走到一个节点或者在原地停留,问他走到4号节点需要平均几分钟?

这是个简单的期望问题,我们用Ei(i=1,2,3,4) 表示从i号节点走到4号节点的数学期望值。根据题意对1号节点有

      E1=(1/3)*E1+(1/3)*E2+(1/3)*E3+1 ①

表示 他下一分钟可以走到2或者3或在原地1,每个可能概率是1/3 ,注意是下一分钟,故要加上1.

同理我们对节点2,3同样可以列出:

      E2=(1/3)*E1+(1/3)*E2+(1/3)*E4+1 ②

      E3=(1/3)*E1+(1/3)*E3+(1/3)*E4+1 ③

那E4等于多少呢? 很明显

      E4=0 ④

因为他就是要到点4

这样上面1234式其实就是组成了一组方程组,解方程组就可得出E1!!,用高斯消元,复杂度是O(n^3)

从上述例子,我们可总结出如何解决期望类问题,根据题意,表示出各个状态的期望(上例的Ei,1234),根据概率公式,列出期望之间的方程,解方程即可。

AC代码:

 #include<stdio.h>
#include<string.h>
double dp[][];
int main()
{
int t;
while(scanf("%d",&t),t)
{
int i,j;
double a,b,c,d,e;
scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);
memset(dp,,sizeof(dp));
dp[t][]=;
for(i=;i<=t-;i++)
{
dp[t][i+]+=(dp[t][i]+);
}
for(i=t-;i>=;i--)
{
dp[i][]+=a*(dp[i+][]+)+b*(dp[i+][]+);
for(j=;j<=i;j++)
dp[i][j]+=c*(dp[i+][j]+)+d*(dp[i+][j+]+)+e*(dp[i][j-]+);
}
printf("%.2lf\n",dp[][]);
}
return ;
}

官方标程:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 1000
#define eps 1e-8
double Atex[MAXN][MAXN];
double a[], b[];
int all;
inline int dcmp(double d) {
return d < -eps ? - : d > eps;
} void gauss(int n, int m)
{
int r,c,i,j;
for(r=c=; r<n&&c<m; r++,c++)
{
for(i=r;i<n;i++)
if(dcmp(Atex[i][c])) break;
if(i==n)//{r--;continue;}
return;
if(i!=r) for(j=c;j<=m;j++) swap(Atex[i][j],Atex[r][j]);
for(i=r+;i<n;i++)
if(Atex[i][c]!=)
{
double temp=Atex[i][c]/Atex[r][c];
for(j=c;j<=m;j++)
Atex[i][j]-=Atex[r][j]*temp;
}
}
for(i=n-;i>=;i--)
{
Atex[i][m]/=Atex[i][i];
Atex[i][i]=;
for(j=i-;j>=;j--) Atex[j][m]-=Atex[i][m]*Atex[j][i];
}
return;
} void makemap(int n) {
memset(Atex, , sizeof(Atex));
all = (+n)*n/;
for (int i = ; i < all; i ++) {
Atex[i][i] = ;
Atex[i][all] = ;
}
int t = , tt;
for (int i = ; i < n-; i ++) {
tt = t + i+;
Atex[t][tt] = -*a[];
Atex[t][tt+] = -*a[];
for (int j = t+; j < tt; j ++) {
Atex[j][j+i+] = -*b[];
Atex[j][j+i+] = -*b[];
Atex[j][j-] = -*b[];
}
t = tt;
}
Atex[t][all] = ;
for (int i = t+; i < all; i ++) {
Atex[i][i-] = -;
}
} int main()
{
int n;
while(scanf("%d", &n) != EOF) {
if(n == ) break;
for (int i = ; i < ; i ++) {
scanf("%lf", &a[i]);
}
for (int i = ; i < ; i ++) {
scanf("%lf", &b[i]);
}
makemap(n);
gauss(all, all);
printf("%.2f\n", Atex[][all]);
}
return ;
}

sdutoj 2623 The number of steps的更多相关文章

  1. SDUT 2623 The number of steps (概率)

    The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a stra ...

  2. SDUT 2623:The number of steps

    The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a stra ...

  3. sdut2623--The number of steps(概率dp第一弹,求期望)

    The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...

  4. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  5. [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  6. Minimum number of steps CodeForces - 805D(签到题)

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  7. Codeforces Round #411 div 2 D. Minimum number of steps

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  8. codeforce 804B Minimum number of steps

    cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each ...

  9. Minimum number of steps 805D

    http://codeforces.com/contest/805/problem/D D. Minimum number of steps time limit per test 1 second ...

随机推荐

  1. C#注意事项及错误处理

    1 使用到config文件配置数据库路径 ConfigurationManager.ConnectionStrings["dbPath"].ConnectionString; db ...

  2. GO语言练习:channel 工程实例

    1.工程代码 2.编译及运行 1.工程目录结构 $ tree cgss cgss ├── cgss.go └── src ├── cg │   ├── centerclient.go │   ├── ...

  3. LongListSelector with bindable SelectedItem

    using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using Microso ...

  4. Sqoop_mysql,hive,hdfs导入导出操作

    前言: 搭建环境,这里使用cdh版hadoop+hive+sqoop+mysql 下载 hadoop-2.5.0-cdh5.3.6.tar.gz hive-0.13.1-cdh5.3.6.tar.gz ...

  5. mac终端命令大全全全全全全全全全

    OSX 的文件系统 OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 US ...

  6. HDU2067/HDU1267 /HDU1130 递推

    小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. 演示对sys用户和普通用户进行审计的示例

    1.确认数据库版本 1对SYS用户审计 1.1配置审计参数 1.2修改liunx日志配置文件 添加以下一列: 1.3 SYS 用户操作演示 2对普通用户审计 2.1配置审计参数 2.2演示对TEST用 ...

  8. BizTalk开发系列(二十二) 开发自定义Map Functoid

    尽管 BizTalk Server 提供许多Functoid以支持一系列不同的操作,但仍可能会遇到需要其他方法的情况.<BizTalk开发系列 Map扩展开发>介绍了通过使用自定义 XSL ...

  9. css属性编写顺序+mysql基本操作+html细节(个人笔记)

    css属性编写顺序: 影响文档流的属性(比如:display, position, float, clear, visibility, table-layout等) 自身盒模型的属性(比如:width ...

  10. python关于列表转为字典的两个小方法

    1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...