Discription
Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the amount of elements where ai > i. For example, the E-value of permutation {1, 3, 2, 4} is 1, while the E-value of {4, 3, 2, 1} is 2. You are requested to find how many permutations of {1, 2, …, N} whose E-value is exactly k.

Input

There are several test cases, and one line for each case, which contains two integers, N and k. (1 <= N <= 1000, 0 <= k <= N). 
Output

Output one line for each case. For the answer may be quite huge, you need to output the answer module 1,000,000,007.

Sample Input

3 0
3 1

Sample Output

1
4

Hint

There is only one permutation with E-value 0: {1,2,3}, and there are four permutations with E-value 1: {1,3,2}, {2,1,3}, {3,1,2}, {3,2,1}

    套路题。
先用dp求出f[i] 为至少有i对满足关系的排列数,然后再二项式反演一下就好啦。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1000000007;
const int maxn=1000;
int jc[maxn+5],ni[maxn+5],n,k,f[maxn+5];
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline int ksm(int x,int y){ int an=1; for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha; return an;}
inline int getC(int x,int y){ return x<y?0:jc[x]*(ll)ni[y]%ha*(ll)ni[x-y]%ha;} inline void init(){
jc[0]=1;
for(int i=1;i<=maxn;i++) jc[i]=jc[i-1]*(ll)i%ha;
ni[maxn]=ksm(jc[maxn],ha-2);
for(int i=maxn;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
} inline void solve(){
memset(f,0,sizeof(f)); f[0]=1;
for(int i=n;i;i--)
for(int j=n-i;j>=0;j--) f[j+1]=add(f[j+1],f[j]*(ll)(n-i-j)%ha);
for(int i=0;i<=n;i++) f[i]=f[i]*(ll)jc[n-i]%ha; int ans=0;
for(int i=k;i<=n;i++)
if((i-k)&1) ans=add(ans,ha-getC(i,k)*(ll)f[i]%ha);
else ans=add(ans,getC(i,k)*(ll)f[i]%ha);
printf("%d\n",ans);
} int main(){
init();
while(scanf("%d%d",&n,&k)==2) solve();
return 0;
}

  

        

HDU - 3664 Permutation Counting的更多相关文章

  1. hdu 3664 Permutation Counting(水DP)

    Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. HDU - 3664 Permutation Counting 排列规律dp

    Permutation Counting Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the ...

  3. HDU 3664 Permutation Counting (DP)

    题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数. 析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的. dp[i][j]表示 i ...

  4. HDU 6880 Permutation Counting dp

    题意: 给你一个n和一个长度为n-1的由0/1构成的b序列 你需要从[1,n]中构造出来一个满足b序列的序列 我们设使用[1,n]构成的序列为a,那么如果ai>ai+1,那么bi=1,否则bi= ...

  5. HDU3664 Permutation Counting

    Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. Hdu 5439 Aggregated Counting (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online 找规律)

    题目链接: Hdu 5439 Aggregated Counting 题目描述: 刚开始给一个1,序列a是由a[i]个i组成,最后1就变成了1,2,2,3,3,4,4,4,5,5,5.......,最 ...

  7. hdu3664 Permutation Counting(dp)

    hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...

  8. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  9. 后缀数组 --- HDU 3518 Boring counting

    Boring counting Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...

随机推荐

  1. 拼接Python字符串最常见的六种方式

    最常见的六种方式拼接Python字符串 字符串是所有编程语言中都有的基本变量的类型,程序员基本每天都在和字符串打交道. 每种字符串拼接方式的使用场景各不相同,我们可以在开发过程中灵活运用. 一.用逗号 ...

  2. MDK editions for Nuvoton devices

    10 Sep 2018 MDK editions for Nuvoton devices For users of Nuvoton devices, Keil® MDK increases its a ...

  3. python字符串、列表和字典的说明

    python字符串.列表和字典的说明 字符串.列表.字典 字符串的作用存储一段数据信息.例如 info = '我爱北京天安门' ,在调取的时候可以直接调取,灵活方便,print(info) 就可以把刚 ...

  4. 十分钟了解HTTPS协议

    概念 HTTP协议上添加一层SSL/TLS协议进行加密,保证用户与web站点之间的数据传输时密文,而不是明文. PS:HTTPS协议 = HTTP协议 + SSL(Secure Sockets Lay ...

  5. python学习--Django mvc框架简介

    让我们一览 Django 全貌 urls.py 网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数. views.py 处理用户发出的请求,从urls. ...

  6. Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识

    今日内容: 1.hash模块2.xml模块3.configparser模块4.sheve 模块5.shutil模块 知识点一:hash什么是hash: hash是一种算法,该算法接受传入的的内容,经过 ...

  7. CodeM美团点评编程大赛初赛A轮

    因为语文太差弃赛,第一个追及问题看不懂我就弃赛了.打进复赛确实挺难的,补一下题,锻炼下就行了. 身体训练 时间限制:1秒 空间限制:32768K 美团外卖的配送员用变速跑的方式进行身体训练.他们训练的 ...

  8. 九度oj 1003

    前几天开刷九度oj,准备把做的题都放上,先放1003 题目1003:A+B             时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:16923 解决:7029 题目描述: 给 ...

  9. 给shell添加颜色

    编辑/etc/baserc 添加 TERM=xterm-color; export TERM alias ls='ls -G' alias ll='ls -lG' 给vim添加颜色 编辑/usr/sh ...

  10. HUST——1106xor的难题之二(异或树状数组单点修改和区间查询)

    1106: xor的难题之二 时间限制: 2 Sec  内存限制: 128 MB 提交: 8  解决: 3 题目描述 上次Alex学长的问题xor难题很简单吧,现在hkhv学长有个问题想问你们. 他现 ...