soj 11599. Tight words

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

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

Input is a sequence of lines, each line contains two integer numbers k and n1 <= n <= 100.

Output

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

Sample Output

100.00000
40.74074
17.38281
0.10130

Problem Source

2014年每周一赛第八场

题意是用0到k这k+1个符号,组成一个n位的数,如果这个n位数相邻的位置上的数字差都不超过,那么这个n位数就称为紧密单词,最后问紧密单词的个数占总数的百分比,因为总数有(k+1)^n之多,所以只能用double来存储了,我们用dp[i][j]表示i位数,最高位上是j的紧密单词的个数,初始化后,很容易就可以递推出来,因为个数比较大,long long也存不下,只能使用double了。

这道题目简单的数位dp,状态转移很简单,处理好边界就可以了。我在写的时候,没看清题目,最后还输出了个百分号,坑死了。

代码如下:

/*************************************************************************
	> File Name: b.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2014年10月21日 星期二 20时14分05秒
 ************************************************************************/

#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>

#define INF (INT_MAX / 10)
#define SQR(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
#define middle(x, y) ((x + y) >> 1)

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

#define N 110

//long long存不下
double dp[N][N];

int main(int argc, char *argv[])
{
	int k, n;


	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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值