hdu2049递归问题

解法:从N中选出M个C[n][m],然后乘上错排公式;f[n]=(n-1)*(f[n-1]+f[n-2]);f[0]=0;f[1]=1;

import java.util.Scanner;

public class hdu2049 {
    public static int C(int a,int b) {
        if (a==b) {
            return 1;
        }else if (b==1) {
            return a;
        }else {
            return C(a-1, b-1)+C(a-1, b);
        }
    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        long[] aa = new long[21];
        aa[1] = 0;
        aa[2] = 1;
        for (int i = 3; i < aa.length; i++) {
            aa[i] =(i-1)*(aa[i-1]+aa[i-2]);
        }        
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int N = sc.nextInt();
            int M = sc.nextInt();
            System.out.println(C(N,M)*aa[M]);
        }
        sc.close();
    }

}

 

posted @ 2024-05-12 17:02  XiaohuangTX  阅读(1)  评论(0编辑  收藏  举报