Zoj 1883-tight words

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.