Uva 10081 Tight words (概率DP)
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 n, 1 <= 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)的更多相关文章
- Substring UVA - 11468 AC自动机+概率DP
题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...
- UVA 10529-Dumb Bones(概率dp)
题意: 给出放一个多米诺骨牌,向左向右倒的概率,求要放好n个骨牌,需要放置的骨牌的期望次数. 分析: 用到区间dp的思想,如果一个位置的左面右面骨牌都已放好,考虑,放中间的情况, dp[i]表示放好前 ...
- UVA 10081 Tight numbers(POJ 2537)
直接看代码就OK.思路比较简单.就是注意概率要在转移过程中算出来.不能算成成立的方案书除以总方案数(POJ的这道题可以这么干.数据很水么.另外POJ要用%.5f,%.5lf 会WA.) #includ ...
- 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 ...
- UVA 11021 C - Tribles(概率DP)
记忆化就可以搞定,比赛里都没做出来,真的是态度有问题啊... #include <iostream> #include<cstdio> #include<cstring& ...
- UVa 11468 (AC自动机 概率DP) Substring
将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...
- uva 11468 AC自动机+概率DP
#include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...
- [uva 11762]Race to 1[概率DP]
引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...
- UVa 11427 Expect the Expected (数学期望 + 概率DP)
题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 p,并且他每局获胜的概率也是 p,但是你最玩 n 局,但是如果比例一直超不过 p 的话,你将不高兴 ...
随机推荐
- 37.创建自定义的指令的限制使用 通过restrict 设置
转自:https://www.cnblogs.com/best/tag/Angular/ 1. 元素名 <runoob-directive></runoob-directive> ...
- springboot 测试类,项目使用shiro时报错UnavailableSecurityManagerException
大概的问题就是,正常运行项目是没有问题的 使用测试类是,加载不了shiro的securityManager,主要导致不是很清楚,望告知, 解决方法 @Resource org.apache.shiro ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- springMVC学习笔记_转载(一)-----springMVC原理
阅读目录 一.什么是springmvc 二.mvc在b/s系统下的应用 三.SpringMVC框架介绍 回到顶部 一.什么是springmvc springMVC是spring框架的一个模块,spri ...
- 将double数据保留两位小数
private double formatDouble(double number) { DecimalFormat df = new DecimalFormat("#.00"); ...
- 一篇文章助你理解Python2中字符串编码问题
前几天给大家介绍了unicode编码和utf-8编码的理论知识,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系.下面在Python2环境中进行代码演示,分别Wind ...
- P4555 [国家集训队]最长双回文串(回文树)
题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 n 的串 S ,求 S 的最长双回文子串 T ,即可 ...
- 安装虚拟机(VM)(一)
原创作品,允许转载,转载时请务必声明作者信息和本声明. https://www.cnblogs.com/zhu520/p/10728248.html 本人小白,有错指出.谢谢! 一:安装虚拟机前奏 ...
- Python中的list,tuple,dict和set
List list的创建与检索 Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 构造list非常简单,直接用 [ ] 把list的所有元素都括 ...
- Liquibase+spring 初步使用
现在的工作的项目中用了liquibase,感觉挺爽的,可以跟踪.管理数据库的重构.这对于很多需求变更较大的项目是非常不错的,特别是互联网的项目.(虽然互联网Nosql已经非常流行,不过俺觉得传统关系型 ...