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. MySql慢查询日志——开启/查看/删除

    1,开启慢查询日志 修改mysql.ini文件,加入如下配置: [mysqld] log-slow-queries=H:\mysql_log\slow_query.log long-query-tim ...

  2. angular ng-bind-html $sce.trustAsHtml

    使用ng-bind-html和$sce.trustAsHtml显示有html符号的内容   angularjs的强大之处之一在于它的双向数据绑定的功能,我们通常会使用data-ng-bind或者dat ...

  3. 【JUC源码解析】AQS

    简介 AQS,也即AbstractQueuedSynchronizer,抽象队列同步器,提供了一个框架,可以依赖它实现阻塞锁和相关同步器.有两种类型,独占式(Exclusive)和共享式(Share) ...

  4. hive读书笔记

    笔记来源<Hive编程指南> 一.hive命令行界面: ‘一次使用’命令:执行一个或多个(分号分隔)查询后hive CLI立即退出: hive -e "select * from ...

  5. SSM-最新pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. 浅析Win8/8.1下安装SQL Server 2005 出现服务项无法正常启动解决方案

    如何才能在微软最新的Windows8/Windows 8.1下正常使用SQL Server 2005套件呢?下面就简单介绍利用文件替换法,解决其服务项无法正常启动的临时方案.当然还是建议使用SQL S ...

  7. 后台可以用layui快速开发

    后台可以用layui快速开发

  8. TW实习日记:第27天

    今天依旧是磨洋工的一天,说真的,被存在各种问题的后端接口把耐心和动力都给磨没了.于是一天就又在沟通接口问题中度过了,完善了一个新功能,将一个新功能开发到了一半.效率可真是够低的,唉.然后不知道为什么突 ...

  9. 复合词 (Compund Word,UVa 10391)

    题目描述: 题目思路: 用map保存所有单词赋键值1,拆分单词,用map检查是否都为1,即为复合词 #include <iostream> #include <string> ...

  10. (转)CGMA - Organic World Building in UE4: week 6

    原文:丢失,这篇是艺术家博客上发现的,小道整理笔记中,临时放于效果案例目录.     In this week we focused on creating the grass and flora t ...