Garlic Jun's new game

In his spare time, Tuantou often brings his colleagues to play games. Recently, Tuantou invented a fun new game: nN  colleagues form a circle, and colleague A holds a rabbit Nini doll in his hand. Mr. Garantou calls the game to start, and each colleague who holds the doll in his hand can choose to pass the doll to the classmates on the left or the right. At this time, the colleague with the doll in his hand is the loser.

After playing for a few rounds, Mr. Garantou thought of a question: How many different ways are there to pass the doll from colleague A, and pass  mAfter m  times, it returned to colleague A. The two methods are regarded as different methods if the colleague who picks up the doll is different, or the order of picking up the doll is different. For example 1->2->3->11>2>3>1 和 1->3->2->11 > 3 > 2 > 1  are two different approaches.

input format

Enter a line with two integers  n,m(3 \leq n \leq 30,1 \leq m \leq 30)n , m ( 3 n 3 0 , 1 m 3 0 ) , indicating a total of nn  colleagues play together and pass mm  times doll.

output format

Output a line, output an integer, indicating how many different ways to pass the baby.

sample input

3 3

Sample output

2

Problem solving instructions: The way to get the doll is from the person on the left or the person on the right.

dp[i][j] represents the number of plans that the i-th doll is passed to the hand labeled j! dp[i][j]=dp[i-1][left label]+dp[i-1][right label];

ac code:

#include<iostream>
#include<cmath>
#include<string.h>
using namespace std;
const int MAXN=35;
int dp[35][35];
int main(){
	int n,m;
	cin>>n>>m;
	memset(dp,0,sizeof(dp));
	dp[1][2]=1;dp[1][n]=1;
	for(int i=2;i<=m;i++){
		for(int j=1;j<=n;j++){
			if(j==1)dp[i][j]=dp[i-1][2]+dp[i-1][n];
			else if(j==n)dp[i][j]=dp[i-1][1]+dp[i-1][n-1];
			else dp[i][j]=dp[i-1][j-1]+dp[i-1][j+1];
		}
	}
	cout<<dp[m][1]<<endl;
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326032140&siteId=291194637