摆放玫瑰花问题
思路:
1、n个位置,k种花摆放方案a[n][k],共有一下两种情况:
1:n-1个位置包括k种花。 共有ka[n-1][k]种方案。
2:n-1个位置包含了k-1种花,剩下的那个位置放剩下的那种花。 共有ka[n-1][k-1]种方案。
2、注意边界:
1.如果k > n||k == 0||n==0,则永远满足不了条件,方案数a[n][k]=0;
2.若k == n ,方案数a[n][k]=k!.
import java.util.Scanner;
public class rose_360 {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int mod = 772235;
int[][] a = new int[n+1][k+1];
int m = full(a,n,k);
System.out.println(m%mod);
}
public static int full(int[][] a, int n, int k){
if(k > n||k == 0||n==0){
a[n][k] = 0;
return a[n][k];
}
if(k == n){
a[n][k] = factorial(k);
return a[n][k];
}else{
a[n][k] = k*full(a,n-1,k)+ k*full(a,n-1,k-1);
}
return a[n][k];
}
public static int factorial(int n){
int sun = 1;
for(int i=1; i<= n; i++){
sun = sun * i;
}
return sun;
}
}