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;
}

最新文章

  1. servlet部分知识总结
  2. python调用windows api
  3. word超链接显示HYPERLINK
  4. 【英语】Bingo口语笔记(26) - Take系列
  5. C语言char[]和char*比较
  6. java编译环境
  7. Deep Learning 学习随记(六)Linear Decoder 线性解码
  8. 阿里云OS和Android的关系(本文转载月光博客)
  9. 【solr基础教程之二】索引
  10. linux查看CPU和内存信息
  11. 在fetch方法中添加header后遇到的预检请求问题
  12. SoapUI简介及下载地址
  13. MySql 使用规范推荐(转)
  14. windows 7/10 安装u盘制作
  15. c代码片段-注解
  16. 关于memcached
  17. bzoj1689 / P1589 [Usaco2005 Open] Muddy roads 泥泞的路
  18. 【Leetcode 136】Single Number
  19. eclipse中查看java源码时,出现source not found问题
  20. 二叉堆(小到大)-数据结构-JavaScript版

热门文章

  1. C#判断是否有中文
  2. opencv学习(三)——绘图功能
  3. 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)
  4. 网口程序udp
  5. stlink 无法再keil中识别 按下复位键可以识别
  6. Oracle中创建DB LINK
  7. YYYY-MM-DD引发的问题
  8. oracle中注释都是问号?中文显示不出来问题
  9. 【Python】【Module】hashlib
  10. 高可靠性——TSN 802.1Qci协议介绍