Luogu P1057 [NOIP2008 Popularization Group] Problem solution of passing game

Not much to say, let's look at the question first:

In physical education class, Xiaoman's teacher often took the students to play games together. This time, the teacher took the students to play the passing game together.

The rules of the game are as follows: n students stand in a circle, and one of them holds a ball in his hand. When the teacher blows the whistle, he starts to pass the ball. Each student can pass the ball to the two students on his left and right. When the teacher blows the whistle again, the passing of the ball stops. At this time, the student who did not pass the ball with the ball is the loser and will perform a show for everyone.

The clever Xiaoman raised an interesting question: How many different ways of passing the ball can make the ball passed from Xiaoman's hands, and after passing m times, return to Xiaoman's hands. Two methods of passing the ball are considered different methods if and only if the sequence of the students receiving the ball in the order of receiving the ball is different in the two methods. For example, there are three students with No. 11, No. 22, and No. 33, and assuming Xiaoman is No. 11, the way to return the ball to Xiaoman after 33 passes is 1→2→3→11→2→3→1 and 1→ 3→2→11→3→2→1, a total of 22 types.

input format

A line with two integers �,�(3≤�≤30,1≤�≤30)n,m(3≤n≤30,1≤m≤30) separated by spaces.

output format

11 integers, indicating the number of methods that meet the meaning of the question.

Input and output samples

Type #1 to copy

3 3

output #1 copy

2

Instructions/Tips

40% of the data satisfies: 3≤�≤30, 1≤�≤203≤n≤30, 1≤m≤20

100% of the data satisfies: 3≤�≤30,1≤�≤303≤n≤30,1≤m≤30

2008 Popularization Group Question 3

I have to say that Luo Gu's topic is still nagging as always. .

So in summary, the idea of ​​this question is still very clear,

Start by finding the rules.
We can find that any position can only be passed from the left and right, then he can only receive the ball from the hands of his classmates on his left and his right, so the number of paths for the ball to pass to him is equal to the number of paths the ball passes to him The sum of the number of paths for the student on the left and the number of paths for the ball to be passed to the student on the right.
This way we can list our equation: f[i][j]=f[i-1][j-1]+f[i-1][j+1];

So here we need to pay special attention, we need to make a special judgment at the position of 1 and n, the boundary

The idea of ​​this question came out, and the code followed. understand

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[35][35];
int main()
{
    int n,m;
    cin>>n>>m;
    a[0][1]=1;  
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
		{
			//考虑特殊 
            if(j==1)   
                a[i][j]=a[i-1][n]+a[i-1][j+1];
            else if(j==n)  
                a[i][j]=a[i-1][j-1]+a[i-1][1];
            else 
                a[i][j]=a[i-1][j-1]+a[i-1][j+1];
        }
    cout<<a[m][1];
    return 0;
}
//本蒟蒻实在太菜,各位宝子们将就着看趴

Family members who understand, give the child a compliment, thank you very much~ 

Guess you like

Origin blog.csdn.net/2301_76331300/article/details/130309423