Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.

We'll call position i (1 ≤ i ≤ n) in permutation p1, p2, ..., pn good, if |p[i] - i| = 1. Count the number of permutations of size n with exactly k good positions. Print the answer modulo 1000000007 (109 + 7).

Input

The single line contains two space-separated integers n and k (1 ≤ n ≤ 1000, 0 ≤ k ≤ n).

Output

Print the number of permutations of length n with exactly k good positions modulo 1000000007 (109 + 7).

Examples

Input
  1. 1 0
Output
  1. 1
Input
  1. 2 1
Output
  1. 0
Input
  1. 3 2
Output
  1. 4
Input
  1. 4 1
Output
  1. 6
Input
  1. 7 4
Output
  1. 328

Note

The only permutation of size 1 has 0 good positions.

Permutation (1, 2) has 0 good positions, and permutation (2, 1) has 2 positions.

Permutations of size 3:

  1. (1, 2, 3) — 0 positions
  2. — 2 positions
  3. — 2 positions
  4. — 2 positions
  5. — 2 positions
  6. (3, 2, 1) — 0 positions

题意:给定N,M,让你求有多少N的排列,使得|a[i]-i|==1的个数为M。

思路:用dp[i][j][now][next];表示前面i个有j个满足上述条件,而且第i为和第i+1位被占的情况位now和next。那么不难写出方程。

但是我写出代码后,发现(2,1)是错的,我输出2,答案是0;因为不可能只有1个在临位。那可以发现,现在是dp[N][M][now][next]*(N-j)!代表的结果是大于M的,还需要容斥。

容斥:dp[N][M]的贡献,减去dp[N][M+1]的贡献,加上...

可以发现,每一个好的位置有M+1个的排列,再算有j个的排列时都会被算M+1次(因为对于这个j+1排列,每一个好位置被无视掉以后都会构成一个j排列)

同理,每一个好的位置有j+2个的排列则会再算j个的排列时重复C(M+2,2)

.... 每一个好的位置有j个的排列会在算i(i < j)的排列时被计算C(M+j,M);

即dp[N][M+j]的系数C(M+j,M);

  1. #include<bits/stdc++.h>
  2. #define rep(i,a,b) for(int i=a;i<=b;i++)
  3. using namespace std;
  4. const int maxn=;
  5. const int Mod=1e9+;
  6. int dp[maxn][maxn][][],fac[maxn],rev[maxn];
  7. int qpow(int a,int x)
  8. {
  9. int res=; while(x){
  10. if(x&) res=1LL*res*a%Mod;
  11. x>>=; a=1LL*a*a%Mod;
  12. } return res;
  13. }
  14. int C(int N,int M) { return 1LL*fac[N]*rev[M]%Mod*rev[N-M]%Mod;}
  15. int main()
  16. {
  17. int N,M;
  18. scanf("%d%d",&N,&M);
  19. fac[]=; rev[]=;
  20. rep(i,,N) fac[i]=1LL*fac[i-]*i%Mod;
  21. rev[N]=qpow(fac[N],Mod-);
  22. for(int i=N-;i>=;i--) rev[i]=1LL*rev[i+]*(i+)%Mod;
  23. dp[][][][]=;
  24. rep(i,,N-) {
  25. rep(j,,i){
  26. rep(p,,){
  27. rep(q,,){
  28. if(!dp[i][j][p][q]) continue;
  29. if(p==&&i) (dp[i+][j+][q][]+=dp[i][j][p][q])%=Mod; //i+1放左边
  30. (dp[i+][j+][q][]+=dp[i][j][p][q])%=Mod; //放右边
  31. (dp[i+][j][q][]+=dp[i][j][p][q])%=Mod; //两边都不放
  32. }
  33. }
  34. }
  35. }
  36. int ans=;
  37. rep(i,M,N){
  38. int tmp=1LL*(dp[N][i][][]+dp[N][i][][])%Mod*C(i,M)%Mod*fac[N-i]%Mod;
  39. if((i-M)%==) {
  40. ans+=tmp;
  41. if(ans>=Mod) ans-=Mod;
  42. }
  43. else{
  44. ans-=tmp;
  45. if(ans<) ans+=Mod;
  46. }
  47. }
  48. printf("%d\n",ans);
  49. return ;
  50. }

CodeForces - 285E: Positions in Permutations(DP+组合数+容斥)的更多相关文章

  1. [Codeforces 1228E]Another Filling the Grid(组合数+容斥)

    题目链接 解题思路: 容斥一下好久可以得到式子 \(\sum_{i=0}^{n}\sum_{j=0}^{n}(-1)^{i+j}C_n^iC_n^j(k-1)^{ni+nj-ij}k^{n^2-(ni ...

  2. Codeforces 285E - Positions in Permutations(二项式反演+dp)

    Codeforces 题目传送门 & 洛谷题目传送门 upd on 2021.10.20:修了个 typo( 这是一道 *2600 的 D2E,然鹅为啥我没想到呢?wtcl/dk 首先第一步我 ...

  3. 青云的机房组网方案(简单+普通+困难)(虚树+树形DP+容斥)

    题目链接 1.对于简单的版本n<=500, ai<=50 直接暴力枚举两个点x,y,dfs求x与y的距离. 2.对于普通难度n<=10000,ai<=500 普通难度解法挺多 ...

  4. CF285E Positions in Permutations(dp+容斥)

    题意,给定n,k,求有多少排列是的 | p[i]-i |=1 的数量为k. Solution 直接dp会有很大的后效性. 所以我们考虑固定k个数字使得它们是合法的,所以我们设dp[i][j][0/1] ...

  5. 【做题】CF285E. Positions in Permutations——dp+容斥

    题意:求所有长度为\(n\)的排列\(p\)中,有多少个满足:对于所有\(i \,(1 \leq i \leq n)\),其中恰好有\(k\)个满足\(|p_i - i| = 1\).答案对\(10^ ...

  6. Codeforces 100548F - Color (组合数+容斥)

    题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...

  7. CodeForces 559C Gerald and Gia (格路+容斥+DP)

    CodeForces 559C Gerald and Gia 大致题意:有一个 \(N\times M\) 的网格,其中有些格子是黑色的,现在需要求出从左上角到右下角不经过黑色格子的方案数(模 \(1 ...

  8. Codeforces Round #345 (Div. 1) A - Watchmen 容斥

    C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...

  9. 【HDOJ5519】Kykneion asma(状压DP,容斥)

    题意:给定n和a[i](i=0..4),求所有n位5进制数中没有前导0且i出现的次数不超过a[i]的数的个数 2<=n<=15000,0<=a[i]<=3e4 思路:设f(n, ...

随机推荐

  1. Java 常用对象-BigInteger类

    2017-11-02 21:57:09 BigInteger类:不可变的任意精度的整数.所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型).BigInteger ...

  2. android--------Android Studio常见问题以及解决方式

    gradle build的时候出现的问题: Error:Execution failed for task ':app:packageDebug'. Duplicate files copied in ...

  3. java.lang.UnsupportedClassVersionError: com/my/test/TestUser : Unsupported major.minor version 52.0

    问题原因: 1.执行代码的jdk版本 低于 编译的jdk版本 2.项目用JDK1.8运行过,现在又在本地的eclipse等开发工具或者本地环境变量为低版本的jdk1.7或者jdk1.6下运行,ecli ...

  4. Sublime text3配置xdebug调试记录

    第一次配置遇到的问题记录: 问题:配置php.ini的时候xdebug.remote_port = 9001刚开始我一直配置9000端口冲突,然后一切弄好了访问浏览器就一直在转圈无法访问: 现在开始配 ...

  5. Css的向左浮动、先右浮动、绝对定位、相对定位的简单使用

    1.div层的浮动 1)div向左浮动.向右浮动 <!doctype html> <html> <head> <meta charset="utf- ...

  6. 064——VUE中vue-router之使用路由别名定制(alias)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. cas 服务端相关配置

    SSO-安全证书配置 CAS默认使用的是HTTPS协议,如果对安全要求不高,可以使用HTTP协议 修改deployerConfigContext.xml(cas/WEB-INF)增加参数 p:requ ...

  8. Git HEAD 意思详解 和版本回退

    首先,Git必须知道当前版本是哪个版本,在git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版 ...

  9. bzoj1677

    题解: 背包 每一个1<<i都是无限量 代码: #include<bits/stdc++.h> using namespace std; ,M=1e9; int n,dp[N] ...

  10. 使用 ADOX 将 Table 添加到 Catalog 时报“类型无效”的原因和解决方法

    http://blog.csdn.net/kfhzy/article/details/6020283 http://blog.csdn.net/kfhzy/article/details/602054 ...