CodeChef - LEMOVIE Little Elephant and Movies
Read problems statements in Mandarin Chineseand 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 P1, P2, ..., 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\n",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;
}
CodeChef - LEMOVIE Little Elephant and Movies的更多相关文章
- CodeChef Little Elephant and Movies [DP 排列]
https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...
- CodeChef LEMOVIE
题意:给你n个数字(下标不同数值相同的数字应当被认为是不同的数字),有n!种排列方式.每种排列方式的价值定义为:第一次出现时比前面的所有数字都大的数值个数. 比如1,2,2,3这个排列中,1,2,3这 ...
- scau 2015寒假训练
并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...
- codechef Little Elephant and Permutations题解
The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...
- CodeChef Little Elephant and Mouses [DP]
https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...
- codechef Little Elephant and Bombs题解
The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...
- CodeChef:Little Elephant and Colored Coins
类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...
- CodeChef Little Elephant and Balance
Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that L ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
随机推荐
- 如何查看 JAR 包的源代码
ava 项目的编译文件经常被打包成 JAR(Java Archive,Java 归档文件)文件,当然,作为学习,有时候也非常想看到这个 JAR 被打包前的源代码是怎么样的. 下面提供几种查看 JAR ...
- node爬虫(简版)
做node爬虫,首先像如何的去做这个爬虫,首先先想下思路,我这里要爬取一个页面的数据,要调取网页的数据,转换成页面格式(html+div)格式,然后提取里面独特的属性值,再把你提取的值,传送给你的页面 ...
- (56)zabbix Screens视图配置
screen翻译成中文为“屏幕”,在超市.单位等等地方都比较常见到监控视频,视频上有多块小视频,实际上zabbix screen和这个功能类似.你可以设置多个screen,每个screen可以显示特定 ...
- 数据结构之--图(Graphics)
1.1:图的定义和术语 图是一种比线性表和树更为复杂的数据结构.在线性表中,数据元素之间仅有线性关系,每个元素仅有一个直接前驱和一个直接后继:在树形结构中,数据元素之间有着明显的层次关系,并且每一 ...
- LIN总线协议
汽车电子类的IC有的采用LIN协议来烧录内部NVM,如英飞凌的TLE8880N和博世的CR665D. LIN总线帧格式如下,一个LIN信息帧有同步间隔.同步域.标示符域(PID域).数据域.校验码域. ...
- ORACLE 检索某列包含特定字符串的数据表工具存储过程
使用示例: delete APPS.FIND_RESULT; set serveroutput ondeclare v_ret varchar(200);begin apps.sp_f ...
- xfce-OpenVAS
OpenVAS开源风险评估系统部署方案 OpenVAS,即开放式漏洞评估系统,是一个用于评估目标漏洞的杰出框架.功能十分强大,最重要的是,它是“开源”的——就是免费的意思啦- 它与著名的Nessu ...
- Python第三方库之openpyxl(6)
Python第三方库之openpyxl(6) 折线图 折线图允许在固定轴上绘制数据,它们类似于散列图,主要的区别在于,在折线图中,每个数据序列都是根据相同的值绘制的,不同的轴可以用于辅助轴,与条形图类 ...
- MHA的介绍和测试(一)
MHA的介绍 MySQL的MHA:MySQL的高级可用性管理器和工具MHA的主要目标是在短(通常为10-30秒)的停机时间内自动化主故障转移和slave升级,不受复制一致性问题的困扰,不需要花费大量的 ...
- Ubuntu安装 Docker CE,VNC访问docker图形界面并安装ROS
从包安装 如果您无法使用Docker的存储库来安装Docker CE,则可以下载.deb适用于您的发行版的 文件并手动安装.每次要升级Docker CE时都需要下载新文件. 安装Docker CE,将 ...