Codeforces 888D: Almost Identity Permutations(错排公式,组合数)
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 \(p_i = 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\).
Examples
Input
4 1
Output
1
Input
4 2
Output
7
Input
5 3
Output
31
Input
5 4
Output
76
题意
给出\(n\)的全排列,求有多少种排列,满足至少\(n-k\)个位置上的数和下标相同(下标从\(1\)开始)
思路
因为\(1\leq k\leq 4\),所以可以将题意转换一下:在\(n\)的全排列中,找到\(k\)个数,数和下标的值全都不相等
我们可以从\(n\)个数中,随机选出\(k\)个数,让这\(k\)个数全都没有放在正确的位置上,选\(k\)个数,我们可以用组合数来求,然后用错排公式来求有多少个数没放在正确的位置上。
因为\(k\)只有四个值,直接计算错排公式的值即可
最后将\(1\)~\(k\)中的这些值加起来即可
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
ll C(int n,int m)
{
ll fenmu=1LL;
ll fenzi=1LL;
for(int i=1;i<=m;i++)
{
fenmu=1LL*fenmu*(n-i+1);
fenzi=1LL*fenzi*i;
}
return fenmu/fenzi;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n,k;
cin>>n>>k;
ll ans=0;
if(k>=1)
ans+=1;
if(k>=2)
ans+=(n*(n-1)/2);
if(k>=3)
ans+=2*C(n,3);
if(k>=4)
ans+=9*C(n,4);
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}
Codeforces 888D: Almost Identity Permutations(错排公式,组合数)的更多相关文章
- Codeforces 888D Almost Identity Permutations:错排公式
题目链接:http://codeforces.com/problemset/problem/888/D 题意: 给定n,k,问你有多少种1到n的排列,满足至少有n-k个a[i] == i. (4 &l ...
- codeforces 340E Iahub and Permutations(错排or容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Iahub and Permutations Iahub is so happy ...
- CodeForces 340E Iahub and Permutations 错排dp
Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对 ...
- HDU 2048:神、上帝以及老天爷(错排公式,递推)
神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 【BZOJ】4517 [Sdoi2016]排列计数(数学+错排公式)
题目 传送门:QWQ 分析 $ O(nlogn) $预处理出阶乘和阶乘的逆元,然后求组合数就成了$O(1)$了. 最后再套上错排公式:$ \huge d[i]=(i-1) \times (d[i-1] ...
- BZOJ4517:[SDOI2016]排列计数(组合数学,错排公式)
Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...
- HDU 1465 不容易系列之一 (错排公式+容斥)
题目链接 Problem Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易,若想永远成功而总从不失败,那更是难上 ...
- hdu 4535(排列组合之错排公式)
吉哥系列故事——礼尚往来 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- HDU——2068RPG的错排(错排公式)
RPG的错排 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
随机推荐
- C语言中的位段----解析
有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位. 例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可. 为了节省存储空间并使处理简便,C语言又提供了一种数据结 ...
- Vue相关,vue.nextTick
vue中有一个较为特殊的API,nextTick.根据官方文档的解释,它可以在DOM更新完毕之后执行一个回调,用法如下: // 修改数据 vm.msg = 'Hello' // DOM 还没有更新 V ...
- mysql外键策略
1.外键 建表时添加外键:constraint 外键名 foreign key 从表字段 references 主表字段 级联操作 create table dage( create table xi ...
- Sqlite 常用操作及使用EF连接Sqlite
Sqlite是一个很轻,很好用的数据库.兼容性很强,由于是一款本地文件数据库,不需要安装任何数据库服务,只需引入第三方开发包就可以.Sqlite的处理速度比MySql和PostgreSQL更快,性能很 ...
- 企业级BI是自研还是采购?
企业级BI是自研还是采购? 上一篇<企业级BI为什么这么难做?>,谈到了企业级BI项目所具有的特殊背景,以及在"破局"方面的一点思考,其中谈论的焦点主要是在IT开发项目 ...
- Jenkins环境变量
目录 一.环境变量 二.自定义环境变量 三.自定义全局变量 四.常用变量定义 五.常用环境变量 一.环境变量 环境变量可以被看作是pipeline与Jenkins交互的媒介.比如,可以在pipelin ...
- spring切面-单线程简单权限判定
spring切面简单模拟用户权限判定 需求: 游客:仅注册用户 用户:修改,注册 管理员:删除,查询,修改,注册 1,文件配置 导包 src下创建applicationContext.xml文件配置如 ...
- linux下编译php扩展
1 在pecl.php.net搜索你需要的php扩展 2 在解压后的扩展目录运行phpize 3 执行编译./configure --with-php-config=/usr/local/php/bi ...
- OpenGL ES2.0 入门经典例子
原文链接地址:http://www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial 免责申明(必读!):本博客提供的所有教程的翻译原稿 ...
- CF570A Elections 题解
Content 有 \(n\) 个候选人和 \(m\) 个城市,每个城市可以给每个候选人投票,已知第 \(i\) 个城市给第 \(j\) 个人投的选票数是 \(a_{i,j}\).我们将第 \(i\) ...