UVa 10081 - Tight Words

題目:有一個集合{0,1,..,k},問其中元素組成的長為n的串中,相鄰元素差值不超過2的串的數比例。

分析:動態規劃、概率dp。

初始狀態:f(i,1)= 1 /(k+1);

狀態轉移:f(i,j)= sum(f(i-1,t))/ (k+1) { 其中,t為和j相差不超過2的元素 }。

說明:好久沒刷題了╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

double dp[101][10];

int main()
{
	int k, n;
	while (cin >> k >> n) {
		for (int i = 0; i <= k; ++ i)
			dp[1][i] = 1.0/(k+1);
		for (int i = 2; i <= n; ++ i)
		for (int j = 0; j <= k; ++ j) {
			dp[i][j] = 0.0;
			for (int t = 0; t <= k; ++ t) {
				if (abs(t-j) > 1) continue;
				dp[i][j] += dp[i-1][t]/(k+1);
			}
		}

		double sum = 0.0;
		for (int i = 0; i <= k; ++ i)
			sum += dp[n][i];
		printf("%.5lf\n",100.0*sum);
	}
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 13:03:58

UVa 10081 - Tight Words的相关文章

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 con

UVA 10081 Tight numbers(POJ 2537)

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

ACM - 递推类题目总结

名曰递推实际上是一类DP计数问题. UVa 10081 -  Tight Words #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <cmath> #define mod 10

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

UVa 10087 - The Tajmahal of ++Y2k

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f