uva10081 Tight Words(DP)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=1022

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define eps 1e-9
using namespace std;
int n,k;
double dp[110][10];
double f(double x){
    return x>-eps ? x:(-x);
}
int main(){
    while(cin>>k>>n){
        memset(dp,0,sizeof dp);
        for(int i=0;i<=k;i++) dp[1][i]=1;
        for(int i=2;i<=n;i++){
            for(int j=0;j<=k;j++){
               for(int now=0;now<=k;now++){
                  if(f(now-j)<=1){
                      dp[i][now]+=dp[i-1][j];
                  }
               }
            }
        }
        double ans=0;
        for(int i=0;i<=k;i++) ans+=dp[n][i];
        for(int i=0;i<n;i++) ans/=(k+1);
        printf("%.5f\n",ans*100);
    }
    return 0;
}
View Code

 

posted @ 2014-02-09 20:35  wonderzy  阅读(119)  评论(0编辑  收藏  举报