Sicily 11599. Tight words
来源:互联网 发布:手机兼职软件 编辑:程序博客网 时间:2024/06/05 09:09
Given is an alphabet {0, 1, ... , k}, 0 <= k <= 9 . We say that a word of length n over this alphabet is tight if any two neighbour digits in the word do not differ by more than 1. Your task is to find out the percentage of tight words of length n over the given alphabet.
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.
4 12 53 58 7
100.0000040.7407417.382810.10130
题意是用0到k这k+1个符号,组成一个n位的数,如果这个n位数相邻的位置上的数字差都不超过1,那么这个n位数就称为tight words,最后问tight words的个数占总数的百分比,因为总数有(k+1)^n之多,所以只能用double来存储了,我们用dp[i][j]表示i位数,最高位上是j的tight words的个数,初始化后,很容易就可以递推出来,因为个数比较大,long long也存不下,只能使用double了。
这道题目简单的数位dp,状态转移很简单,处理好边界就可以了。
// Problem#: 11599// Submission#: 3667449// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>double dp[111][111];int main(int argc, char *argv[]){ int k, n; int N=111; while (scanf("%d%d",&k,&n)!=EOF){ for (int i = 0; i < N; ++i){ for (int j = 0; j < N; ++j){ dp[i][j] = 1.0; } } for (int i = 2; i<N; ++i){ for (int j = 0; j<= k;++j){ dp[i][j] = dp[i-1][j]; if (j > 0){ dp[i][j] += dp[i-1][j-1]; } if (j < k) { dp[i][j] += dp[i-1][j+1]; } } } double ans = 0; double base = pow(k + 1, n); for (int i = 0; i <= k; ++i) { ans += dp[n][i]; } printf("%.5f\n", ans * 100.0 / base); } return 0;}
- Sicily 11599. Tight words
- Sicily 11599. Tight words
- soj 11599. Tight words
- UVA 10081 Tight Words
- ZOJ 1883 Tight Words
- UVA 10081 Tight Words
- UVa 10081 - Tight Words
- zoj 1883 - Tight Words
- Tight words (dp)
- FOJ 1342 Tight Words
- Tight Words(数位DP)
- UVa 10081 - Tight Words
- POJ 2537 Tight words DP
- uva 10081 - Tight Words(dp)
- UVa 10081 Tight Words (DP)
- uva 10081 (Tight Words) (DP)
- UVA10081-Tight Words(递推)
- UVA10081 - Tight Words(dp)
- .NET学习之010203总结
- Splay终极模板
- Android 5.x OTA Update官方文档(五、Recovery系统下分区映射)
- 原码 补码 反码及进制转换
- vs2013安装xna4.0模板
- Sicily 11599. Tight words
- An Easy Problem
- Mac os x 10.10上使用quicktime录制iphone屏幕
- java连接各种数据库的代码大全
- 备忘:Integer.valueOf抛出NumberFormatException
- 优秀管理者的判断标准
- Ubuntu下Sublime Text 3解决无法输入中文的方法
- 黑马程序员-iOS视频学习之五
- 第15题