Time limit: 3.000 seconds

Given is an alphabet {0, 1, ... , k}, 0
<= k <= 9 . We say that a word of length n over this alphabet is tightif
any two neighbour digits in the word do not differ by more than 1.

Input is a sequence of lines, each line contains two integer numbers k and n1 <= n <= 100. 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

4 1
2 5
3 5
8 7

Output for the sample input

100.00000
40.74074
17.38281
0.10130

题意:给定两个数k,n。

用 {0,
1, ... , k}的数组成一个n个数的序列。假设这个序

列每两个相邻的数相差<=1,就记为是tight,求这样的序列占总序列的比率。

思路: dp[i][j]表示第i为数字是j的概率 。

即   dp[i][j] = 1/(k+1) *  (dp[i-1][j-1]+dp[i-1][j] + dp[i+1][j] );

注意下边界就OK了。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=105; double dp[maxn][15],t,ans;
int n,k; void initial()
{
memset(dp,0,sizeof(dp));
t=1.0/(k+1),ans=0.0;
for(int j=0; j<=k; j++) dp[1][j]=t;
} void solve()
{
for(int i=2; i<=n; i++)
for(int j=0; j<=k; j++)
{
dp[i][j]+=t*dp[i-1][j];
if(j!=0) dp[i][j]+=t*dp[i-1][j-1];
if(j!=k) dp[i][j]+=t*dp[i-1][j+1];
}
for(int j=0; j<=k; j++) ans+=dp[n][j];
printf("%.5lf\n",ans*100);
} int main()
{
while(scanf("%d %d",&k,&n)!=EOF)
{
initial();
solve();
}
return 0;
}

最新文章

  1. Python标准库13 循环器 (itertools)
  2. 【MongoDB】MongoDB 3.2 SCRAM-SHA-1验证方式
  3. Octopus系列之价格计算公式
  4. LInux内核分析——计算机是如何工作的进行
  5. Java学习笔记 第一章 入门&lt;转&gt;
  6. nodejs 基本类型和语法
  7. extjs panel自动滚动
  8. oracle 中控制文件中到底记录了哪些信息
  9. winform 多线程中ShowDialog()步骤无效的解决办法
  10. 【剑指offer】面试题28:字符串的排列
  11. 从Linux终端管理进程:10个你必须知道的命令
  12. jQuery Uploadify上传插件
  13. 去培训机构参加IT培训值不值
  14. Markdown语法你都会了吗?
  15. [LeetCode] Dota2 Senate 刀塔二参议院
  16. Linux的发行版,不同发行版之间的联系和区别
  17. java之日志管理
  18. java自动化学习笔记
  19. [蛙蛙推荐]SICP第一章学习笔记-编程入门
  20. hikari链接池

热门文章

  1. php中命名空间和use
  2. 构造函数的理解(构造函数与 init 方法)
  3. 集合区别(list和linkedlist的区别)?
  4. 使用ShareSDK分享-图片的链接
  5. css hover图片hover效果兼容ie8
  6. Ubuntu18.04 更改GRUB引导菜单背景图片和默认启动项
  7. 求第区间第k大数 TLE归并树
  8. CSU 1364 Interview RMQ
  9. mysql中lock tables与unlock tables(锁表/解锁)使用总结
  10. Oracle拾遗