UVA 10081 - Tight Words (数论 dp)
来源:互联网 发布:金昌印花软件 编辑:程序博客网 时间:2024/05/29 05:01
Problem B: Tight words
Given is an alphabet {0, 1, ... , k}, 0 <= k <= 9 .We say that a word of lengthn over this alphabet istight if any two neighbour digits in the word do not differby more than 1.Input is a sequence of lines, each line contains two integer numbersk andn, 1 <= n <= 100. For eachline of input, output the percentage of tight words of lengthn over the alphabet{0, 1, ... , k} with 5 fractional digits.
Sample input
4 12 53 58 7
Output for the sample input
100.0000040.7407417.382810.10130题意:给定k,n。要求用0-k的数字组成长度为n的序列中,相邻两两差小于等于1的概率。
思路:dp,dp[i][j]表示组成长度为i,最后一个数字为j的符合条件的序列的概率。那么每次多一个数字的时候,只要加上dp[i][j - 1],dp[i][j + 1],dp[i][j],然后在除上多取一个数字的种数(k + 1)即可。
代码:
#include <stdio.h>#include <string.h>int k, n, i, j;double dp[105][10];int main() {while (~scanf("%d%d", &k, &n)) {memset(dp, 0, sizeof(dp));for (j = 0; j <= k; j ++)dp[1][j] = 1.0 / (k + 1);for (i = 2; i <= n; i ++) {for (j = 0; j <= k; j ++) {dp[i][j] += dp[i - 1][j];if (j) dp[i][j] += dp[i - 1][j - 1];if (j != k) dp[i][j] += dp[i - 1][j + 1];dp[i][j] /= (k + 1);}}double ans = 0;for (j = 0; j <= k; j ++)ans += dp[n][j];printf("%.5lf\n", ans * 100);}return 0;}
- UVA 10081 - Tight Words (数论 dp)
- uva 10081 - Tight Words(dp)
- Uva 10081 Tight words (概率DP)
- uva 10081 - Tight Words(dp)
- UVa 10081 Tight Words (DP)
- uva 10081 (Tight Words) (DP)
- UVA 10081 Tight Words
- UVA 10081 Tight Words
- UVa 10081 - Tight Words
- UVa 10081 - Tight Words
- UVA10081 - Tight Words(dp)
- UVA10081 Tight Words(dp)
- Uva 10081 - Tight Words 解题报告(递推)
- ZOJ 1883 Tight Words(概率DP)
- Tight words (dp)
- Tight Words(数位DP)
- POJ 2537 Tight words DP
- fzoj 1342 Tight Words(DP:递推)
- BOOL 和bool
- 2013.11.09
- 七。内核字符驱动
- 《directx游戏开发终极指南》光盘中代码编译失败的三个问题
- 关于《大数据时代》
- UVA 10081 - Tight Words (数论 dp)
- 八。内核中的中断机制
- 电脑常见的接口大全
- 数据恢复软件设计与实现(三)
- 玩转myeclipse
- ios7新特性--2
- java swing 图片按钮的使用
- JS编程技巧
- 讨厌的字符串读入