Question: If each letter of a word does not differ by 1, it is called closeness. We will give you a letter set {0 ~ K },
A word with a length of N is a close probability.
Analysis: probability DP. The probability of the characters at the end position is the State DP.
Status: set f (I, j) as the word with the length of I, and take the close probability from the set {0,..., k;
Transfer: f (I, j) = (f (I-1, J-1) + f (I, j) + f (I, j + 1)/(k + 1 );
Note ).
#include <iostream>#include <cstdlib>#include <stdio.h>usingnamespace std;double F[ 101 ][ 10 ];int main(){ int k,n; while ( cin >> k >> n ) { double r = 1.0/(1+k); for ( int i = 0 ; i <= k ; ++ i ) F[ 1 ][ i ] = r; for ( int i = 2 ; i <= n ; ++ i ) for ( int j = 0 ; j <= k ; ++ j ) { F[ i ][ j ] = F[ i-1 ][ j ]*r; if ( j > 0 ) F[ i ][ j ] += F[ i-1 ][ j-1 ]*r; if ( j < k ) F[ i ][ j ] += F[ i-1 ][ j+1 ]*r; } double sum = 0.0; for ( int i = 0 ; i <= k ; ++ i ) sum += F[ n ][ i ]; printf("%.5lf\n",sum*100); } return 0;}
Zoj 1883-tight words