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 的话,你将不高兴 ...
随机推荐
- 用html语言写一个功课表
今天在网上看了一个关于html的教程,主要是讲表格,看完之后认为有必要上机试试.于是就写了以下的一段代码. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb ...
- POJ 3187 全排列+杨辉三角(组合数)
思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm& ...
- Android 自定义的开关按钮——SwitchButton
本文转自:http://blog.csdn.net/swust_chenpeng/article/details/19967501 我将原文的控件进行了一些修改,去掉了原来控件的外边框,只留下重要的遮 ...
- WebSocket handshake: Unexpected response code: 404
在执行 http://www.cnblogs.com/best/p/5695570.html 提供的 websocket时候, 报错了 “WebSocket handshake: Unexpe ...
- UVa 12661 Funny Car Racing【 dijkstra 】
题意:给出n个点,m条路,每条路用5个整数表示u,v,a,b,t u表示这条路的起点,v表示终点,a表示打开时间,b表示关闭时间,t表示通过这条道路需要的时间 看的紫书,因为边权不再仅仅是路上的时间, ...
- ora_tool
#!/bin/ksh # # Copyright (c) 1998, 2002, Oracle Corporation. All rights reserved. # version() { ...
- iOS网络缓存机制
iOS的网络引擎自带缓存机制: 网络请求在经过网络引擎时有过处理(添加了字段),所以用api的网络请求无法获取缓存. [NSURLCache sharedURLCache]
- shiro整合thymeleaf
1.引入依赖 <!--thymeleaf中使用shiro--> <dependency> <groupId>com.github.theborakompanioni ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- poj2031-Building a Space Station(最小生成树,kruskal,prime)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5874 Accepte ...