Almost Identity Permutations CodeForces
来源:互联网 发布:uml软件建模过程 编辑:程序博客网 时间:2024/06/01 22:54
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
- Almost Identity Permutations CodeForces
- codeforces 888D.Almost Identity Permutations
- CodeForces 888D Almost Identity Permutations
- D. Almost Identity Permutations(dp)
- codeforces 888D Almost Identity Permutations (组合数+错排)
- Permutations CodeForces
- codeforces 863F Almost Permutation
- Codeforces 903D Almost Difference
- codeforces 903D. Almost Difference
- CodeForces 513B1 Permutations
- Codeforces 124B. Permutations
- CodeForces 124B Permutations
- Codeforces 513B Permutations
- CodeForces-124B Permutations
- Codeforces 513B2 Permutations
- codeforces 189C Permutations
- codeforces-26A-Almost Prime【分解质因数】
- Codeforces 256A Almost Arithmetical Progression【dp】
- Echarts(二、柱状图(各参数详细描述))
- 一、java项目常用工具类之加密工具类
- JAVA线程小结
- CodeForces
- 杭电ACM OJ 1004 Let the Balloon Rise 考察map和list的遍历
- Almost Identity Permutations CodeForces
- Python——安装编译环境
- 字母大小转换及其十进制的ASCLL码值
- 时钟
- python语法基础(一)
- 后台开发面试常被问到的问题(一):什么之间的区别
- laravel上传文件报错:413 Request Entity Too Large
- jq中 text() html() val()
- 使用nexus搭建maven仓库(本地私服)