码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 3664 Permutation Counting

时间:2018-04-20 23:45:09      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:nbsp   max   tput   scan   define   hdu   class   mount   answer   

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

标签:nbsp   max   tput   scan   define   hdu   class   mount   answer   

原文地址:https://www.cnblogs.com/JYYHH/p/8893737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!