Almost Identity Permutations CodeForces - 888D (错排公式)

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值