Luogu P1057 [NOIP2008 Popularity Group] Passing Game

Luogu P1057 [NOIP2008 Popularity Group] Passing Game

Idea: Because the number of passes of a person is derived from the number of times he left and right other people, so a[i][j]=a[i-1][j-1]+a[i-1][j+ 1].
Code

#include<bits/stdc++.h>
using namespace std;
long long n,m,a[31][31],i,j;
int main(){
    
    
	cin>>n>>m;
	a[0][1]=1;
	for(i=1;i<=m;i++)
		for(j=1;j<=n;j++){
    
    
			if(j==1)
				a[i][j]+=a[i-1][2]+a[i-1][n];
			else if(j==n)
				a[i][j]+=a[i-1][n-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;
}

Guess you like

Origin blog.csdn.net/weixin_52536621/article/details/113921530