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;
}

Uva 10081 Tight words (概率DP)的更多相关文章

  1. Substring UVA - 11468 AC自动机+概率DP

    题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...

  2. UVA 10529-Dumb Bones(概率dp)

    题意: 给出放一个多米诺骨牌,向左向右倒的概率,求要放好n个骨牌,需要放置的骨牌的期望次数. 分析: 用到区间dp的思想,如果一个位置的左面右面骨牌都已放好,考虑,放中间的情况, dp[i]表示放好前 ...

  3. UVA 10081 Tight numbers(POJ 2537)

    直接看代码就OK.思路比较简单.就是注意概率要在转移过程中算出来.不能算成成立的方案书除以总方案数(POJ的这道题可以这么干.数据很水么.另外POJ要用%.5f,%.5lf 会WA.) #includ ...

  4. UVA 10529 - Dumb Bones (概率dp)

    题目描述 You are trying to set up a straight line of dominos, standing on end, to be pushed over later f ...

  5. UVA 11021 C - Tribles(概率DP)

    记忆化就可以搞定,比赛里都没做出来,真的是态度有问题啊... #include <iostream> #include<cstdio> #include<cstring& ...

  6. UVa 11468 (AC自动机 概率DP) Substring

    将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...

  7. uva 11468 AC自动机+概率DP

    #include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...

  8. [uva 11762]Race to 1[概率DP]

    引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...

  9. UVa 11427 Expect the Expected (数学期望 + 概率DP)

    题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 p,并且他每局获胜的概率也是 p,但是你最玩 n 局,但是如果比例一直超不过 p 的话,你将不高兴 ...

随机推荐

  1. C++基础之全局变量

    C++的水比較深,之前我一直以为C++的全局变量会像其它语言一样,很easy仅仅要在头文件里,定义一个变量就可以,比方以下的test.h: #ifndef _TEST_H #define _TEST_ ...

  2. dig linux下的使用

    一般来说linux下查询域名解析有两种选择,nslookup或者dig,而在使用上我觉得dig更加方便顺手. 如果是在debian下的话,只要装上dnsutils这个包就可以使用dig命令了. 最基本 ...

  3. poj 1094 / zoj 1060 Sorting It All Out

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26876   Accepted: 92 ...

  4. Liunx-php7安装swoole扩展

    Liunx-php7安装swoole扩展 标签(空格分隔): php 下载包 https://github.com/swoole/swoole-src/releases 安装过程 直接wget也行直接 ...

  5. Android实现App版本自动更新

    现在很多的App中都会有一个检查版本的功能.例如斗鱼TV App的设置界面下: 当我们点击检查更新的时候,就会向服务器发起版本检测的请求.一般的处理方式是:服务器返回的App版本与当前手机安装的版本号 ...

  6. 集合HashSet的使用

    集合中的HashSet底层是通过Hash表实现,HashSet的特点是元素唯一,但用到Hash表就跟hashCode()有了密不可分的联系,所以HashSet的唯一性是通过hashCode()方法来保 ...

  7. List<List<model>>如何更快捷的取里面的model?

    访问接口返回数据类型为List<List<model>>,现在想将其中的model插入数据库,感觉一点点循环有点傻,0.0...,各位有没有其他的方法? List<Lis ...

  8. IBM主机上清除告警黄灯方法

    机器亮黄灯告警一般是有硬件问题(单电源等可能有安全隐患的硬件配置也可能造成黄灯亮起),见到后首先进行下硬件诊断,方法如下: 诊断系统,判断是否硬件故障:1.Root用户执行diag: 2.回车后,进第 ...

  9. 6、json支持

    package main import ( "encoding/json" "fmt") // Json 支持 type Response1 struct{ P ...

  10. [SDOI2012]Longge的问题 欧拉反演_欧拉函数

    Code: #include<cstdio> #include<algorithm> #include<cmath> #include<string> ...