UVA10081 Tight Words(dp)

Given is an alphabet {0,1,…,k}, 0 ≤ k ≤ 9. We say that a word of length n over this alphabet is tight if any two neighbour digits in the word do not differ by more than 1. Input Input is a sequence o
摘要由CSDN通过智能技术生成

Given is an alphabet {0,1,…,k}, 0 ≤ k ≤ 9. We say that a word of length n over this alphabet is tight if any two neighbour digits in the word do not differ by more than 1.
Input
Input is a sequence of lines, each line contains two integer numbers k and n, 1 ≤ n ≤ 100. Output
For each line of input, output the percentage of tight words of length n over the alphabet {0, 1, …, k} with 5 fractional digits.
Sample Input
41 25
35 87
Sample Output
100.00000
40.74074
17.38281
0.10130

题目大意:
一串数长度为n,由0~k 的数字组成。且所有相邻两个数字之差的绝对值不大于1。问符合这个要求的数 占所有长度为n的数的百分比。

思路:
还是从最简单的情况,1个数,2个数,3个数开始思考。。。于是定义:长度为i的tight words 有f[i]个。但是发现决定f[i]的因素有第i位本身是多少,,和第i-1位上的数是多少,,,于是就想到定义为二维的状态。f[i][j]表示第i位为j时,长度为i的tight words有多少个。
方程:f[i][j]=f[i-1][j-1]+f[i-1][j]+f[i-1][j+1];
边界条件:f[1][j]=1;(0<=j<=k);
答案:f[n][0-k]的和。
需要注意的是,因为要求得是百分比,如果直接把分母(k+1)^n算出来的话太大了,但是经过计算分子是可以存下的。所以就先用dp算出分子,再用不断的除以k就好了。
但是!!最重要的是精度问题!!!!!

这是第一种输出方法,f[i][j]定义的整数,在计算tot的时候转化为double来除以k

const 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值