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
 

Source

这个题也是蛮基础的 但是最后纠结于n-5~n这个范围的计算把自己整恶心了

其实要是从n-1一直推到0就很好地避开了这个问题 n-1 是由n推来 

刚刚在纠结为毛要if(i+l<=n)    dp[i]+=dp[i+l];其实不这么写也没问题→_→提交还快了11ms

但是要理解为毛这推 实际上就是在前一道题的基础上多了一个航线 可以直接飞  其他的 没区别了

/********
2015.10.12-2015.10.13
hdu4405
46MS 2916K 913B C++
31MS 2884K 931B C++
********/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
double dp[100005];
int n,m,x,y;
int step[100005];
int main()
{
    //freopen("cin.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        if(m==0&&n==0) break;
        memset(dp,0,sizeof(dp));
        memset(step,-1,sizeof(step));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            step[x]=y;
        }
        for(int i=n-1;i>=0;i--)
        {
            if(step[i]!=-1) {
                dp[i]=dp[step[i]];
                continue;
            }
            for(int l=1;l<=6;l++)
            {
               // if(i+l<=n)
                    dp[i]+=dp[i+l];
                //else break;

            }
            dp[i]=dp[i]/6+1;
        }
        printf("%.4f\n",dp[0]);
    }
    return 0;
}