Sicily 11599. Tight words

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 nover 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年每周一赛第八场

// Problem#: 11599
// Submission#: 3525803
// 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 <stdio.h>
#include <math.h>

double dp[10][101];
double ans[10][101];
    
int main() {
    
    double sum = 0;
    
    for (int i = 0; i <= 9; i++) {
        
        for (int k = 0; k <= i; k++) dp[k][1] = ans[k][1] = 1;
        for (int j = 2; j <= 100; j++) {
            dp[0][j] = dp[0][j - 1] + (i > 0 ? dp[1][j - 1] : 0);
            for (int k = 1; k < i; k++) 
                dp[k][j] = dp[k - 1][j - 1] + dp[k][j - 1] + dp[k + 1][j - 1];  // recursion formula
            dp[i][j] = (i > 0 ? dp[i - 1][j - 1] : 0) + dp[i][j - 1];
            for (int k = 0; k <= i; k++) sum += dp[k][j];
            ans[i][j] = sum / pow(1.0 * i + 1, j);
            sum = 0;
            if (ans[i][j] < 0.0000001) break;  // ans[i][j] will decrease when j increase. If the ans[i][j] is small enough to ignore, it's unnecessary to calculate anymore
        }
    }

    int k, n;
    while (scanf("%d%d", &k, &n) != EOF) printf("%.5f\n", ans[k][n] * 100);

    return 0;
}                                 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值