[Ybtoj High-Efficiency Advanced 1.1] [Recursion] Passing game

[Ybtoj High-Efficiency Advanced 1.1] [Recursion] Passing game

topic

Insert picture description here
Insert picture description here


Problem-solving ideas

Set f [i] [j] denotes the number of ways in the first individual i j passes to get the ball
in accordance with clear meaning of the questions you want to get the ball i-th
ball must first i-1 times next to this person
f [i][j]=f[i-1][j-1]+f[i+1][j-1]
and these n people form a circle,
so the first person is from the nth person and the ith person +1 person
receives the ball while the nth person receives the ball from the 1st person and the n-1th person


Code

#include<iostream>
#include<cstdio>
int n,m,f[50][50];
using namespace std;
int main()
{
    
    
	scanf("%d%d",&n,&m);
	f[1][0]=1;
	for (int j=1;j<=m;j++)
	    for (int i=1;i<=n;i++)
	    {
    
    
	        if (i==1) f[i][j]=f[n][j-1]+f[i+1][j-1];
			if (i==n) f[i][j]=f[i-1][j-1]+f[1][j-1];
			if (i>1&&i<n) f[i][j]=f[i-1][j-1]+f[i+1][j-1]; 
		}
    printf("%d\n",f[1][m]);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/111639245