/*
Migrated from Lutece 1521 Passing the Ball
Description
There are N people passing the ball. It starts from the first person,
and each time the person who gets the ball should pass the ball to another one.
So what is the number of situations in which the ball is passed back to the first person after passing M times.
Input
Including two integers N and M, and N∈[2,9],M∈[1,15].
Output
The answer,and it does not exceed 2^31.
*/
/*
N个人
最后一次传球应传给第一个人,也就是说最后一次传球时,球不能再第一个人手上
即倒数第二次传球时,球不能传给第一个人,其余的情况,球可以传给任何人
倒数第二次传球,球若再第一个人身上,则可以传给N-1个人
若倒数第二次传球时,球不在第一个人身上,则可以传给N-2个人
倒数第二次传球时,球在第一个人的情况,可以视为传M-2次球的情况数
倒数第二次传球时,球不在第一个人的情况,
此时若传给第一个人可以视为传M-1次球的情况数(传M-1次球,最后一次传球时,球只能传给第一个人)
f(x)是传x次球的情况数,
则f(x+1) = f(x-2)*(N-1)+f(x-1)*(N-2)
*/
#include <iostream>
#include <vector>
int countBallPassingWays(int& personCount,int passCount,std::vector<int>& memo){
if(passCount==1){
return 0;
}else if(passCount==2){
return personCount-1;
}
if(memo[passCount]!=-1){
return memo[passCount];
}
return countBallPassingWays(personCount,passCount-2,memo)*(personCount-1)+countBallPassingWays(personCount,passCount-1,memo)*(personCount-2);
}
int main(){
int personCount,passCount;std::cin>>personCount>>passCount;
std::vector<int> memo(passCount+1,-1);
//用于储存i次球的情况数,避免重复计算
//这题personCount不会变化,所以不需要二维数组
std::cout<<countBallPassingWays(personCount,passCount,memo)<<std::endl;
return 0;
}