A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.
Let’s call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that pi = i.
Your task is to count the number of almost identity permutations for given numbers n and k.
Input
The first line contains two integers n and k (4 ≤ n ≤ 1000, 1 ≤ k ≤ 4).
Output
Print the number of almost identity permutations for given n and k.
Example
Input
4 1
Output
1
Input
4 2
Output
7
Input
5 3
Output
31
Input
5 4
Output
76
这题说有index-k个数要求和index一致,那么其他和自己的index不一致,当然是错排公式了,但是这个错排最多只算到4个。。。
其他枚举组合们就是组合数了。。。用java了,组合数挺大的。
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
static BigInteger fac[]=new BigInteger[1005];
static BigInteger w[]=new BigInteger[6];
static BigInteger get(int n,int m)
{
return fac[n].divide(fac[m]).divide(fac[n-m]);
}
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
fac[0]=BigInteger.ONE;
for(int i=1;i<=n;i++)
fac[i]=fac[i-1].multiply(BigInteger.valueOf(i));
w[0]=BigInteger.ONE;
w[1]=BigInteger.ZERO;
w[2]=BigInteger.ONE;
for(int i=3;i<=5;i++)
w[i]=BigInteger.valueOf(i-1).multiply(w[i-1].add(w[i-2]));
BigInteger ans=BigInteger.ZERO;
for(int i=n-k;i<=n;i++)
{
ans=ans.add(get(n,i).multiply(w[n-i]));
}
System.out.println(ans);
}
}