HDU 1723 Distribute Message DP

The contest’s message distribution is a big thing in prepare. Assuming N students stand in a row, from the row-head start transmit message, each person can transmit message to behind M personals, and how many ways could row-tail get the message?

InputInput may contain multiple test cases. Each case contains N and M in one line. (0<=M<N<=30)
When N=0 and M=0, terminates the input and this test case is not to be processed.
OutputOutput the ways of the Nth student get message.
Sample Input
4 1
4 2
0 0
Sample Output
1
3


        
 
Hint
4 1 : A->B->C->D
4 2 : A->B->C->D, A->C->D, A->B->D


OJ-ID:
HDU-1723

author:
Caution_X

date of submission:
20190930

tags:
DP

description modelling:
给定N,M,表示有1~N个人排成一列,前一个人可以向后M个人传递消息,问第N个人有多少种方式接收第一个人传出的消息

major steps to solve it:
dp[i]:=第i个人接收消息的方式有几种
if(i-1<=M) dp[i]++;
dp[i+j]+=dp[i]; j∈{1,2,3......M}.

AC CODE:
#include<stdio.h>
#include<string.h>

int dp[31],M,N;

int main()
{
    //freopen("input.txt","r",stdin);
    while(scanf("%d%d",&N,&M)!=EOF&&(N!=0||M!=0)){
        int i,j;
        memset(dp,0,sizeof(dp));
        dp[1]=1;
        for(i=2;i<=N;i++){
            if(i-1<=M){
                dp[i]++;
            }
            for(j=1;j<=M&&i+j<=N;j++){
                dp[i+j]+=dp[i];
            }
        }
        printf("%d\n",dp[N]);
    }
    return 0;
}
View Code

 





posted on 2019-09-30 13:54  Caution_X  阅读(98)  评论(0编辑  收藏  举报

导航