目录

蓝桥杯第二天2022国赛-第一题-小蓝与钥匙

目录

蓝桥杯第二天:2022国赛 第一题 小蓝与钥匙

https://i-blog.csdnimg.cn/direct/466bb3068ae7477e8376afcbc22318b2.png

	    public static void main(String[] args) {
//	        全错位排列要求所有元素均不在原位上。对于 n 个元素,其错排数 D(n) 的递推公式为:
	//
//	        D(n)=(n−1)⋅[D(n−1)+D(n−2)]
	            int m=28,n=14;
	            long result=C(m,n)*D(14);//从28个数中取14个数有C(m,n)不同的取法,而每一种取法都有D(14)种错位排序法
	            System.out.println(result);

	    }
	    public  static long  C(int m,int n)
	    {
	        long ans=1;// 从m个数中取n个数,有多少种取法 比如从1-28个数中取14个数有多少种取法
	        for(int i=m;i>=m+1-n;i--) //(28 *   27*.....15)/(14*13*....1)
	        {
	            ans*=i;
	        }
	        for(int i=n;i>=1;i--)
	        {
	            ans/=i;
	        }
	        return  ans;
	    }
	    public static long  D(int n)
	    {
	        if(n==1)
	        {
	            return  0;
	        }
	        if(n==2)
	        {
	            return  1;
	        }
	        else{
	            return  (n-1)*(D(n-1)+D(n-2));
	        }
	    }

两个公式:1、从1-m数中取n个数一个有多少种取法    (m*(m-1)*…(n+1)) / n!

2、全错位排列要求所有元素均不在原位上。对于 n 个元素,其错排数 D(n) 的递推公式为:

D(n)=(n−1)⋅[D(n−1)+D(n−2)]   其中D(1) = 0  D(2)= 1