Problem Description

Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.

There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0<Xi<Yi<=N) without throwing the dice. If there is another flight line from Yi, Hzz can take the flight line continuously. It is granted that there is no two or more flight lines start from the same grid.

Please help Hzz calculate the expected dice throwing times to finish the game.

Input

There are multiple test cases.

Each test case contains several lines.

The first line contains two integers N(1≤N≤100000) and M(0≤M≤1000).

Then M lines follow, each line contains two integers Xi,Yi(1≤Xi<Yi≤N).

The input end with N=0, M=0.

Output

For each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.

Sample Input

2 0

8 3

2 4

4 5

7 8

0 0

Sample Output

1.1667

2.3441

Description(CHN)

在一个 \(1*n\) 的格子上掷色子,从 \(0\) 点出发,掷了多少前进几步,同时有些格点直接相连,即若 \(a\) ,\(b\) 相连,当落到 \(a\) 点时直接飞向 \(b\) 点。求走到 \(n\) 或超出 \(n\) 期望掷色子次数

\(n≤100000\)

Solution

期望倒推

设 \(f[i]\) 表示当前到达第 \(i\) 号点,距离游戏结束的期望是多少

显然,\(f[n]=f[n+1]=...=f[n+5]=0\)

然后反着枚举 \(i\) ,如果当前点有直通机,就直接等于直通的那个点的期望

否则,枚举这一次骰子的点数,\(f[i]=\sum_{x=1}^6\frac{f[i+x]+1}{6}\)

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100000+10;
int n,m,fly[MAXN];
db f[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m)break;
memset(fly,-1,sizeof(fly));
for(register int i=1;i<=m;++i)
{
int u,v;read(u);read(v);
fly[u]=v;
}
for(register int i=0;i<=n+5;++i)f[i]=0.0;
for(register int i=n-1;i>=0;--i)
if(!(~fly[i]))
for(register int x=1;x<=6;++x)f[i]+=f[i+x]/6.0+1/6.0;
else f[i]=f[fly[i]];
printf("%.4f\n",f[0]);
}
return 0;
}

【刷题】HDU 4405 Aeroplane chess的更多相关文章

  1. HDU 4405 Aeroplane chess 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...

  2. hdu 4405 Aeroplane chess(简单概率dp 求期望)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 4405 Aeroplane chess(概率+dp)

    Problem Description Hzz loves aeroplane chess very much. The chess map contains N+ grids labeled to ...

  5. [ACM] hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...

  6. HDU 4405 Aeroplane chess 概率DP 难度:0

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 明显,有飞机的时候不需要考虑骰子,一定是乘飞机更优 设E[i]为分数为i时还需要走的步数期望,j为某个可能 ...

  7. HDU 4405 Aeroplane chess(期望)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:从0走到n,每次走之前掷一次筛子,掷出几点就向前走几点,走到大于等于n的地方就停止.但是, ...

  8. HDU 4405 Aeroplane chess:期望dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意: 你在下简化版飞行棋... 棋盘为一个线段,长度为n. 上面有m对传送门,可以直接将你从a ...

  9. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

随机推荐

  1. Kafka在高并发的情况下,如何避免消息丢失和消息重复?kafka消费怎么保证数据消费一次?数据的一致性和统一性?数据的完整性?

    1.kafka在高并发的情况下,如何避免消息丢失和消息重复? 消息丢失解决方案: 首先对kafka进行限速, 其次启用重试机制,重试间隔时间设置长一些,最后Kafka设置acks=all,即需要相应的 ...

  2. Javascript闭包例子

    闭包的概念 内层的函数可以引用存在于包围它的函数内的变量,即使外层函数的执行已经终止.可理解为,闭包就是能够读取其他函数内部变量的函数. 表现形式是:定义在函数内部的函数. function f1() ...

  3. HTML5心得

    1. 在做登录或搜索框的时候,可以为input加上autofocus属性,这样打开页面焦点自动在登录框或搜索框中.减少用户不必要的定位点击. 如<label>Search:<inpu ...

  4. 三 Hive 数据处理 自定义函数UDF和Transform

    三  Hive 自定义函数UDF和Transform 开篇提示: 快速链接beeline的方式: ./beeline -u jdbc:hive2://hadoop1:10000 -n hadoop 1 ...

  5. SpringBoot入门(二)——起步依赖

    本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...

  6. 使用jenkins构建一个maven项目

    1.登陆到jenkins首页,创建项目-->选择maven-->输入项目名称-->选择项目类型 2.进入项目配置:{先写一下项目描述和设置下保留的历史构建,然后向下拉} 找到源吗管理 ...

  7. 在deepin系统中制作桌面快捷方式

    在使用deepin-wine 安装一些软件的时候,每次启动都需要到.deepinwine目录下运行deepin-wine xx.exe.笔者在安装过HeidiSql之后,一直苦于这种情况.比较好的解决 ...

  8. 227. Mock Hanoi Tower by Stacks【LintCode java】

    Description In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different si ...

  9. 如何处理 jQuery $(window).resize() 中的方法被多次执行的小问题

    引言: 估计很多同志们在编写浏览器resize()的方法时,都会遇到这样的情况: 当拖动浏览器的边角时,页面中的一些效果随浏览器大小的改变而触发,这一过程开始到结束,resize() 中的方法被执行了 ...

  10. How Does Batch Normalization Help Optimization?

    1. 摘要 BN 是一个广泛应用的用于快速稳定地训练深度神经网络的技术,但是我们对其有效性的真正原因仍然所知甚少. 输入分布的稳定性和 BN 的成功之间关系很小,BN 对训练过程更根本的影响是:它让优 ...