NOIP 2008 passing game

Luo Gu P1057 passing game

Luo Gu Portal

JDOJ 1536: [NOIP2008] Game ball T3

JDOJ Portal

Description

Gym class when the teacher Xiao Man often took students to play games together. This time, teachers do with students passing game together.

Rules of the game is this: n a classmate standing in a circle, in which the hands of a student holding a ball, when the teacher blows the whistle to start the ball at midnight, each student can pass the ball around their own two classmates one (right or left), a teacher at midnight to blow the whistle again, passing stop, this time, holding the ball did not spread to the students is the loser, to give you a performance.

Smart Xiao Man raises an interesting question: How many different ways to make a pass from the hands of Xiao Man began to pass the ball, pass the m times later, he returned to the hands of Xiao Man. Pass two kinds of methods are considered as a different method, if and only if these two methods, the sequence of the students receiving the ball catch according to the order of the composition is different. For example there are three students No. 1, No. 2, No. 3 and No. 1 is assumed Xiao Man, the ball back to the three methods are Xiaoman hand 1-> 2-> 3-> 1 and 1-> 3- > 2-> 1, a total of two kinds.

Input

Input common line, there are two spaces separated by an integer n, m (3 <= n <= 30,1 <= m <= 30).

Output

The output common line, there is an integer number of labeling methods in line with the meaning of the questions.

Sample Input

3 3

Sample Output

2

HINT

40% of the data satisfies: 3 <= n <= 30 1 <= m <= 20

100% data satisfies: 3 <= n <= 30 1 <= m <= 30

Source

NOIP2008 popular group

answer:

Recursive.

I think the solution to a problem moving return of only four parts:

1, Status: setting \ (dp [i] [j ] \) represents the elapsed \ (J \) passes back (I \) \ Number of program numbers.

2, the initial value: \ (DP [. 1] [0] =. 1 \) . It can be understood in conjunction with the above-described state.

3. Transfer: \ (DP [I] [J] DP = [. 1-I] [J-. 1] + DP [I +. 1] [-J. 1] \) in order to process a cyclic structure, we need special handling. \ (i = 1 \) and \ (i = n \) case.

4, the answer is: \ (DP [. 1] [m] \) .

Code:

#include<cstdio>
using namespace std;
int n,m;
int dp[31][31];//dp[i][j]表示经过j次传球传回i号人的方案数
int main()
{
    scanf("%d%d",&n,&m);
    dp[1][0]=1;
    for(int i=1;i<=m;i++)
    {
        dp[1][i]=dp[n][i-1]+dp[2][i-1];
        for(int j=2;j<n;j++)
            dp[j][i]=dp[j-1][i-1]+dp[j+1][i-1];
        dp[n][i]=dp[n-1][i-1]+dp[1][i-1];
    }
    printf("%d",dp[1][m]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11387657.html