• CodeChef


    Read problems statements in Mandarin Chinese and Russian.

    Little Elephant from Zoo of Lviv likes to watch movies.

    There are N different movies (numbered from 0 to N − 1) he wants to watch in some order. Of course, he will watch each movie exactly once. The priority of ith movie is Pi.

    A watching of a movie is called exciting if and only if one of the following two conditions holds:

    • This is the first watching.
    • The priority of this movie is strictly greater than the maximal priority of the movies watched so far.

    Let us call the number of exciting watchings the excitingness of the order.

    Help him to find the number of different watching orders whose excitingness does not exceed K. Since the answer can be large, print it modulo 1000000007 (109+7).

    Input

    The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow.

    The first line of each test case contains two space-separated integers N and K. The next line contains N space-separated integers P1P2, ..., PN.

    Output

    For each test case, print the number of different watching orders having at most Kexcitingness modulo 1000000007 (109+7).

    Constraints

    • 1 ≤ T ≤ 10
    • 1 ≤ K ≤ N ≤ 200
    • 1 ≤ Pi ≤ 200

    Example

    Input:
    2
    3 1
    3 1 2
    4 3
    1 2 2 3
    
    Output:
    2
    24
    

    Explanation

    In the first case, there are two boring watching orders whose excitingness not greater than K=1[3, 1, 2][3, 2, 1]. Both watching orders have one excitingwatching: the first watching.

    In the second case, every watching order has at most 3 excitingness.

        一般的排列问题都可以转化成把元素以一定顺序插入进序列来做。这个题如果按照升序来插入的话,后插入的元素会挡住前面插入的元素,没法算;

    而如果按照降序插入的话,影响是很好计算的:插入的元素不会挡住前面的,而且只有放在最前面会对 激动值 +1。

        有很多重复的元素的话,我们就可重集组合一下,不过对激动值的 影响最大还是1,因为相同元素也会挡住。

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=405;
    const int ha=1000000007;
    int jc[maxn],ni[maxn],T,n,k;
    int dp[maxn],num[maxn],N,f[maxn];
    
    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 void getC(){
    	jc[0]=1;
    	for(int i=1;i<=400;i++) jc[i]=jc[i-1]*(ll)i%ha;
    	ni[400]=ksm(jc[400],ha-2);
    	for(int i=400;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
    }
    
    inline int C(int x,int y){ return x<y?0:jc[x]*(ll)ni[y]%ha*(ll)ni[x-y]%ha;}
    
    inline void init(){
        memset(dp,0,sizeof(dp));
        memset(num,0,sizeof(num));
    }
    
    inline void solve(){
    	dp[0]=1;
    	for(int i=200,A=0,P=0,tot,lef;i;i--) if(num[i]){
    		lef=C(A+num[i]-1,num[i])*(ll)jc[num[i]]%ha,tot=add(C(A+num[i],num[i])*(ll)jc[num[i]]%ha,ha-lef);
    		
    		memset(f,0,sizeof(f));
    		for(int j=min(P,k);j>=0;j--){
    			f[j+1]=add(f[j+1],dp[j]*(ll)tot%ha);
    			f[j]=add(f[j],dp[j]*(ll)lef%ha);
    		}
    		memcpy(dp,f,sizeof(f));
    		
    		P++,A+=num[i];
    	}
    	
    	int ans=0;
    	for(int i=0;i<=k;i++) ans=add(ans,dp[i]);
    	printf("%d
    ",ans);
    }
    
    int main(){
    	getC();
    	scanf("%d",&T);
    	while(T--){
    		init(),scanf("%d%d",&n,&k);
    		for(int i=1;i<=n;i++) scanf("%d",&N),num[N]++;
    		solve();
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    C语言程序设计第四次作业
    C语言程序设计第三次作业
    C语言程序设计第二次作业
    python网络编程 day34 网络编程——线程的相关概念及开启线程
    python网络编程 day33 网络编程——进程拾遗,互斥锁(重要)生产者消费者模型
    python网络编程 day32 网络编程——进程multiprocessing模块及join用法
    文件上传下载代码
    python网络编程 day30 网络编程——hmac模块与hashlip、socketserver、粘包问题
    python网络编程 day29 网络编程初识 ——tcp与udp、struck模块
    python网络编程 day28 网络编程初识 ——socket
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8853624.html
Copyright © 2020-2023  润新知