zoukankan      html  css  js  c++  java
  • HDU

    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
    ",ans);
    }
    
    int main(){
    	init();
    	while(scanf("%d%d",&n,&k)==2) solve();
    	return 0;
    }
    

      


  • 相关阅读:
    桥梁模式
    原型模式
    css backgroundposition
    eclipse配置了maven,项目报错
    SQL 练习题目
    Springmvc + Ibatis 搭建 点餐系统
    Delphi 性能优化工具
    Delphi的接口陷阱
    delphi 内存管理,定期释放
    Delphi制作数据感知控件之浮想联翩
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8893737.html
Copyright © 2011-2022 走看看