[Ybt] [Basic calculation recurrence class example 4] Passing game

Passing game

Topic link: passing game


Title description

Insert picture description here
Insert picture description here

Problem solving ideas

This question is relatively simple.
Obviously a person can get the ball from both sides of him, then it is also possible to get contributions from both sides.
The recurrence formula is:
fi, j = fi − 1, j − 1 + fi − 1, j + 1 f_{ {i},{j}}=f_{ {i-1},{j-1}} +f_{ {i-1},{j+1}}fi,j=fi1,j1+fi1,j+1
Give f 0, 1 f_{ {0},{1}}f0,1Attached 1 1The initial value of 1 ,j = nj=nj=n orj = 0 j=0j=A special sentence is sufficient at 0:00 .

code

#include<iostream>
#include<cstdio>
#define int long long
using namespace std;

int n,m;
int f[40][40];

signed main()
{
    
    
	cin>>n>>m;
	f[0][1]=1;
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
		{
    
    
			if(j==1)
				f[i][j]=f[i-1][n]+f[i-1][2];
			else if(j==n)
				f[i][j]=f[i-1][n-1]+f[i-1][1];
			else
				f[i][j]=f[i-1][j+1]+f[i-1][j-1];
		}
	cout<<f[m][1]<<endl;
}

Guess you like

Origin blog.csdn.net/SSL_guyixin/article/details/111701482