Code:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 100000 + 233;
const double perc = (1 / 6.0);
double f[maxn];
int vis[maxn];
int main()
{
int n,m;
while(1)
{
memset(vis, -1, sizeof(vis));
memset(f,0, sizeof(f));
scanf("%d%d",&n,&m);
if(!n) break;
for(int i = 1;i <= m; ++i)
{
int a,b;
scanf("%d%d",&a,&b);
vis[a] = b;
}
for(int i = n - 1;i >= 0;--i)
{
if(vis[i] != -1){ f[i] = f[vis[i]] ; continue; }
for(int j = 1;j <= 6; ++j) f[i] += (f[i + j] + 1) * perc;
}
printf("%.4lf\n",f[0]);
}
return 0;
}