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);
}
}

最新文章

  1. Python全栈开发 线程和进程
  2. windows dos命令窗口的环境变量
  3. 模拟 POJ 2996 Help Me with the Game
  4. 为什么socket编程要用到多线程
  5. FTP搭建
  6. PID控制器的数字实现及C语法讲解
  7. 7.OpenACC
  8. CollapsingToolbarLayout
  9. chrome 关闭自己主动更新
  10. 在高德地图应用api,和api展出的标记小的应用程序
  11. hightmaps 按地图上显示的统计数据
  12. Android Camera 调用流程总结
  13. MySQL连接数据库报时区错误:java.sql.SQLException: The server time zone value
  14. [国嵌攻略][054][NandFlash驱动设计_写]
  15. 用Git的hooks实现项目的自动部署
  16. 关于flutter插件地图的使用flutter_map
  17. 005dayPython学习:编写并执行Pythong代码和流程梳理
  18. String、StringBuilder、StringBuffer的区别
  19. poj2480(利用欧拉函数的积性求解)
  20. LeetCode 521 Longest Uncommon Subsequence I 解题报告

热门文章

  1. html5 canvas 填充渐变形状
  2. [转载]DOMContentLoaded与interactive
  3. 【转】XMPP_3920_最靠谱的中文翻译文档
  4. iOS 在viewDidLayoutSubviews自动布局crash问题
  5. A*算法改进——Any-Angle Path Planning的Theta*算法与Lazy Theta*算法
  6. 洛谷 P2089 烤鸡
  7. lucene查询索引之Query子类查询——(七)
  8. mac 安装gevent报错
  9. 数组的splice方法
  10. docker 要点学习