Back to Square 1

题目连接:

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/back-to-square-1

Description

The game “Back to Square 1” is played on a board that has n squares in a row and n-1 probabilities. Players take turns playing. On their first turn, a player advances to square 1.After the first turn, if a player is on square i , the player advances to square i + 1 with probability p(i) , and returns to square 1 with probability 1-p(i) .The player is finished upon reaching square n .

Note: If you are having trouble solving this problem, you can refer to the video on IEEE Academic's Xtreme website, which provides an explanation of this problem.

Input

The input is made up of multiple test cases. Each test case contains 2 lines of input.

The first line in each test case is an integer n , 1 <= n <= 1,000, which represents the number of squares for this test case.

On the next line are n -1 single-space separated floating point numbers, each greater than 0 and less than or equal to 1, representing p(1) , p(2) , p(3) , ..., p(n-1) , respectively.

The input will end with a 0 on a line by itself.

Note: If for an input test case n=1 (i.e. there is only one square) then there will be no following line since there will be no probabilities. For example, the following input:

2

0.5

1

3

0.1 0.2

0

contains in total 3 test cases. The first one having 2 squares with an in-between transition probability equal to 0.5, the second test case consists of a single square (and thus no transition probabilities are provided) and the last test case consists of 3 squares with respective transition probabilities equal to 0.1 and 0.2 .

Output

For each test case, output the expected number of turns needed to reach the final state, rounded to the nearest integer. You are guaranteed that the expected number of turns will be less than or equal to 1,000,000.

Note: Every line of output should end in a newline character .

Sample Input

3

0.5 0.25

0

Sample Output

13

Hint

题意

有n个格子,你有p[i]的概率前进一步,你有(1-p[i])的概率滚回一号点

问你期望走到n号点的概率是多少

题解

倒推之后,就能发现一个傻逼规律

而不像我一样,傻傻的去写高斯消元。。。

代码

 #include<bits/stdc++.h>
using namespace std;
const int maxn = 1050;
const double eps = 1e-7;
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
double dp[2][maxn],ans;
double p[maxn];
int n,m,x,y,now;
int main()
{
while(cin>>n)
{
if(n==0)break;
ans=0;
memset(dp,0,sizeof(dp));
dp[0][1]=1.0;
for(int i=1;i<n;i++)cin>>p[i];
double now = 1;
double ans = 1;
for(int i=n-1;i>=1;i--)
{
now/=p[i];
ans+=now;
}
printf("%.0f\n",ans);
}
}

Xtreme8.0 - Back to Square 1 数学的更多相关文章

  1. Xtreme8.0 - Magic Square 水题

    Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...

  2. Xtreme8.0 - Kabloom 动态规划

    Xtreme8.0 - Kabloom 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/kablo ...

  3. Xtreme8.0 - Kabloom dp

    Xtreme8.0 - Kabloom 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/kablo ...

  4. TensorFlow2.0(二):数学运算

    1 基本运算:加(+).减(-).点乘(*).除(/).地板除法(//).取余(%) 基本运算中所有实例都以下面的张量a.b为例进行: >>> a = tf.random.unifo ...

  5. Codeforces 715A. Plus and Square Root[数学构造]

    A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. Xtreme8.0 - Sum it up 水题

    Sum it up 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/sum-it-up Descr ...

  7. Xtreme8.0 - Play with GCD dp

    Play with GCD 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/play-with-g ...

  8. UVA 11461 - Square Numbers 数学水题

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. Xtreme9.0 - Mr. Pippo's Pizza 数学

    Mr. Pippo's Pizza 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pipp ...

随机推荐

  1. prefab内容分析

    写在前面: 当前使用的unity版本:5.3.7p4. 如果打开prefab文件是乱码: 把editer的asset Srialization改为Force Text即可. 一.什么是Prefab P ...

  2. Tensorflow中的变量

    从初识tf开始,变量这个名词就一直都很重要,因为深度模型往往所要获得的就是通过参数和函数对某一或某些具体事物的抽象表达.而那些未知的数据需要通过学习而获得,在学习的过程中它们不断变化着,最终收敛达到较 ...

  3. jinja模板语言

    模板 要了解jinja2,那么需要先理解模板的概念.模板在Python的web开发中广泛使用,它能够有效的将业务逻辑和页面逻辑分开,使代码可读性增强.并且更加容易理解和维护. 模板简单来说就是一个其中 ...

  4. Ubuntu14.04搭建Android O编译环境

    一.搭建环境 官方参考文档: 1.代号.标签和版本号 2.Factory Images 3.Driver Binaries 4.工具链  软硬件版本: 1.系统平台:I5-8500T+8G+1T,Ub ...

  5. VBScript操作SFTP

    示例代码主要通过VBScript实现对SFTP的上传下载功能 ' Return yyyyMM base on current date Function FormatCurrentDate() Cur ...

  6. Wiggle Sort I & II

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  7. 如何生成能在没有安装opencv库及vs2010环境的电脑上运行的exe文件

    项目基本算法已经完成,甲方需要一个可以运行的demo.目前,程序能在自己的电脑上正常运行.移植到其他win7系统上,运行失败. 寻找各种解决办法,baidu找到两个办法: 1.使用静态链接的方法,这种 ...

  8. find中的-print0和xargs中-0的奥妙【转】

    find cygnus/firmware_cygnus/target/linux/brcm5830/files/arch/arm/mach-iproc/pm_iproc/ -name "*. ...

  9. stylus项目知识点

    1.在项目中,引入.sty文件的时候,用来下面方式 @import "~common/stylus/variable.styl" ~ 是stylus的写法,参考https://gi ...

  10. 2018-2019-2 网络对抗技术 20165301 Exp1 PC平台逆向破解

    任务一 直接修改程序机器指令,改变程序执行流程 1.输入指令objdump -d pwn5301 | more反汇编pwn1文件. 通过反汇编,可以看到main函数中的call 804891,其机器码 ...