codeforces 888D Almost Identity Permutations (组合数+错排)
来源:互联网 发布:淘宝嘉年华有假货吗 编辑:程序博客网 时间:2024/06/05 00:27
传送门:codeforces 888D
题目大意:
对于 1~n 的所有排列中满足至少有 n-k 个元素的下标和元素值相同的有多少个。
思路:
至少有 n-k 个元素下标和元素值相同,也就是至多有 k 个元素下标和元素值不相同。注意,是不可能只存在 1 个元素的元素下标和元素值不相同的。
所以,我们可以从 n 个数中取出 i (0<= i <=k && i != 1)个数让它们互相交换顺序,使得它们的元素值和元素下标不相同。共有 C(n,i) 种取法,还要乘以它们交换顺序的方法,也就是错排数。
但是由于 k<=4 ,所以可以不用这么麻烦,从给定的样例可以得知 k=3 时错排数为 2,k=4 时错排数为 9,其他时候为 1. 怎么得出的呢?样例 5 3 为 31,5 4为 76,只多了个 C(5,4)=5,但是结果却增加了 45,所以 k=4 是错排数为 9. 当然错排数也可以利用公式直接得出。
所以只要求出 C(n,i) * 错排数 的和就可以了。注意 i 不能为 1,因为不可能只存在一个元素的下标和值不同。
代码:
#include<stdio.h>#include<string.h>typedef long long LL;LL c[1010][6];void C(){ //求组合数 int i,j;c[0][0]=1;for(i=0;i<1005;i++) c[i][0]=1;for(i=1;i<1005;i++)for(j=1;j<5&&j<=i;j++){if(j==i) c[i][j]=1;else c[i][j]=c[i-1][j-1]+c[i-1][j];}}int main(){int i,n,k;LL ans;C();while(~scanf("%d%d",&n,&k)){ans=0;for(i=0;i<=k;i++){if(i==3) ans+=c[n][i]*2; //组合数 * 错排数 else if(i==4) ans+=c[n][i]*9;else if(i!=1) ans+=c[n][i];}printf("%lld\n",ans);}return 0;}
阅读全文
1 0
- codeforces 888D Almost Identity Permutations (组合数+错排)
- codeforces 888D.Almost Identity Permutations
- CodeForces 888D Almost Identity Permutations
- Almost Identity Permutations CodeForces
- D. Almost Identity Permutations(dp)
- 排错+组合数
- HDU 2068(错排+组合数)
- RPG的错排(组合数+错排)
- codeforces 341C Iahub and Permutations(组合数dp)
- Codeforces 903D Almost Difference
- codeforces 903D. Almost Difference
- Codeforces-830D Singer House(组合数+dp)
- Codeforces 340E Iahub and Permutations【思维+错排Dp】
- [BZOJ4517][Sdoi2016]排列计数(错排+组合数)
- bzoj4517 [Sdoi2016]排列计数 (错排 + 组合数)
- [组合数+bell数] codeforces 569D. Symmetric and Transitive
- codeforces 903D Almost Difference(递推式)
- codeforces Educational Round 34 D Almost Difference
- JS 实现完美include
- 3-3 构造函数的调用(高级)
- 一个简单的自适应调整字体大小view
- 04 HTTP协议中的get/post方法
- 散列3 QQ帐户的申请与登陆
- codeforces 888D Almost Identity Permutations (组合数+错排)
- 秒杀系统设计
- Eclipse装AmaterasUML插件
- HDU2036
- windows主机与虚拟机Linux共享文件夹
- ReactNative安卓端的打包发布
- leetcode729. My Calendar I
- 简单电路
- Keras-3 Keras With Otto Group