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 ...
随机推荐
- 日常Java 2021/10/14
Java数据结构 Java BitSet类 BitSet类创建一种特殊类型的数组来保存位值,数组大小随需要增加,BitSet(),BitSet(int size) 其中的方法 void and(Bit ...
- 试了下GoAsm
在VC里我们: #include <windows.h> DWORD dwNumberOfBytesWritten; int main() { HANDLE hStdOut = GetSt ...
- Zookeeper之创建组,加入组,列出组成员和删除组
public class CreateGroup implements Watcher { private static final int SESSION_TIMEOUT=5000; //ZooKe ...
- win10产品密钥 win10永久激活密钥(可激活win10所有版本)
https://www.win7w.com/win10jihuo/18178.html#download 很多人都在找2019最新win10永久激活码,其实win10激活码不管版本新旧都是通用的,也就 ...
- 转 序列化Serializable和Parcelable的区别详解
什么是序列化,为什么要进行序列化 答:对象要进行传输(如:activity 与activity间 ,网络间 进程间等等).存储到本地就必须进行序列化 . 这种可传输的状态就是序列化. 怎么序列化??两 ...
- Linux学习 - 使用qq邮箱发送邮件
1 打开qq邮箱,设置->账户->POP3/SMTP,开启服务 2 配置/etc/mail.rc文件 set from=73***32@qq.com #设置发送方邮件地址 set smtp ...
- Android Bitmap 全面解析(二)加载多张图片的缓存处理
一般少量图片是很少出现OOM异常的,除非单张图片过~大~ 那么就可以用教程一里面的方法了通常应用场景是listview列表加载多张图片,为了提高效率一般要缓存一部分图片,这样方便再次查看时能快速显示~ ...
- Output of C++ Program | Set 4
Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 1 #include<iostream ...
- vue引入d3
单页面使用 cnpm install d3 --save-dev 指定版本安装 cnpm install d3@6.3.1 -S <script> import * as d3 from ...
- 类型类 && .class 与 .getClass() 的区别
一. 什么是类型类 Java 中的每一个类(.java 文件)被编译成 .class 文件的时候,Java虚拟机(JVM)会为这个类生成一个类对象(我们姑且认为就是 .class 文件),这个对象包含 ...