Description

对于一个1->n的排列 ,定义A中的一个位置i是好的,当且仅当Ai-1>Ai 或者Ai+1>Ai。对于一个排列A,假如有不少于k个位置是好的,那么称A是一个好的排列。
现在有q个询问,每个询问给定n,k,问有多少排列是好的。答案对10^9+7取模。
 

Input

输入文件名为permutation.in。
首先输入q。
接下来输入q个询问n,k 。

Output

输出文件名为permutation.out。
输出q行,每行一个整数代表答案。
 

Sample Input

8
4 3
6 4
10 7
20 14
50 40
100 72
1000 900
3000 2000

Sample Output

8
448
1433856
868137807
908422882
609421284
150877522
216180189
 

Data Constraint

对于20%的数据,n<=10,q=1
对于40%的数据,n<=20,q=1
对于60%的数据,n<=100
对于100%的数据,n,k<=3000,q<=10000
 

正难则反,我们可以考虑下“坏数字”(谷峰)($a_{i-1} < a_{i}>a_{i+1}$)。数位动规,数字的位置变换我们可以想象成插入,令F[i][j]表示到第i个数字,此时有j个谷峰的合法插入方案数,则第i个数字如果查到j个坏数字的两侧,则不会产生新的“谷峰”,那么有2*j个位置,F[i][j]*(2*j)-->F[i+1][j],如果插到其他位置,则会产生一个新的“谷峰”(序列两侧可以分别假想有个0),那么有(i-2*j)个位置,F[i][j]*(i-(2*j))-->F[i+1][j+1].初始条件F[1][1]=1,最后$ans=\sum _{i=1}^{n-k}f\left[ n\right] \left[ i\right]$

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define qaq 1000000007
using namespace std;
int n,k,q;
long long f[][];
long long ans;
int main(){
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
f[][]=;
for (int i=;i<=;i++)
for (int j=;j<=;j++)
if (!f[i][j])
f[i][j]=f[i-][j]*(*j)%qaq+f[i-][j-]*(i-*(j-))%qaq;
scanf("%d",&q);
while (q--){
scanf("%d%d",&n,&k);;
ans=;
for (int i=n-k;i>=;i--)
ans=(ans+f[n][i])%qaq;
printf("%lld\n",ans);
}
return ;
}

神奇的代码

(事实上我们可以预处理n,k到3000的值最后再直接累加即可)

JZOJ.5235【NOIP2017模拟8.7】好的排列的更多相关文章

  1. [jzoj 5343] [NOIP2017模拟9.3A组] 健美猫 解题报告 (差分)

    题目链接: http://172.16.0.132/senior/#main/show/5343 题目: 题解: 记旋转i次之后的答案为$ans_i$,分别考虑每个元素对ans数组的贡献 若$s_i& ...

  2. JZOJ 5235. 【NOIP2017模拟8.7A组】好的排列

    5235. [NOIP2017模拟8.7A组]好的排列 (File IO): input:permutation.in output:permutation.out Time Limits: 1000 ...

  3. JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠

    JZOJ [NOIP2017提高A组模拟9.14]捕老鼠 题目 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕 ...

  4. JZOJ 5246. 【NOIP2017模拟8.8A组】Trip(trip)

    5246. [NOIP2017模拟8.8A组]Trip(trip) (File IO): input:trip.in output:trip.out Time Limits: 1500 ms Memo ...

  5. JZOJ 5236. 【NOIP2017模拟8.7A组】利普希茨

    5236. [NOIP2017模拟8.7A组]利普希茨 (File IO): input:lipschitz.in output:lipschitz.out Time Limits: 1000 ms ...

  6. JZOJ 5230. 【NOIP2017模拟A组模拟8.5】队伍统计

    5230. [NOIP2017模拟A组模拟8.5]队伍统计 (File IO): input:count.in output:count.out Time Limits: 1500 ms Memory ...

  7. JZOJ【NOIP2013模拟联考14】隐藏指令

    JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...

  8. JZOJ.5281【NOIP2017模拟8.15】钦点

    Description

  9. [jzoj 5178] [NOIP2017提高组模拟6.28] So many prefix? 解题报告(KMP+DP)

    题目链接: https://jzoj.net/senior/#main/show/5178 题目: 题解: 我们定义$f[pos]$表示以位置pos为后缀的字符串对答案的贡献,答案就是$\sum_{i ...

随机推荐

  1. j2ee高并发时使用全局变量需要注意的问题

    原文:https://blog.csdn.net/jston_learn/article/details/21617311 开发中,全局变量的使用很频繁,但对于多线程的访问,使用全局变量需要注意的地方 ...

  2. 阿里云云盘扩容数据盘_Linux

    随着业务的增长,您的数据盘容量可能无法满足数据存储的需要,这时您可以使用 磁盘扩容 功能扩容数据盘.   说明 挂载在实例上的数据盘,只有当实例处于 运行中 (Running) 或 已停止(Stopp ...

  3. 改动文件后缀的C语言实现

    ,其他配置项保持一致.         step 3: 在"Old2New"目录下新建名为"update.bat"的批处理文件,该文件的内容为: ChangeS ...

  4. 本体论与OWL

    http://semanticweb.org/wiki/Main_Page.html http://owl.man.ac.uk/documentation.shtml  https://zh.wiki ...

  5. unity, Global和Local编辑模式

    下图表示是在Local模式下: 下图表示是在Global模式下: 不要搞反.

  6. 【Android界面实现】View Animation 使用介绍

        转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992     我们能够使用view animation 动画系统来给View控件加入tween动画(下称& ...

  7. error: Semantic Issue: Interface type cannot be statically allocated

    转自:http://hongmin118.iteye.com/blog/1333524 error: Semantic Issue: Interface type cannot be statical ...

  8. Http basic Auth 认证方式帮助类

    BasicAuthenticationUtil import java.io.IOException; import java.security.MessageDigest; import javax ...

  9. 413. Reverse Integer【easy】

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  10. 无语的Filezilla

    之前也一直用filezilla,今天遇到个奇葩问题:在2008R2上装完filezilla server,本打算生成个证书用于SSL加密,没想到一直报错"Failed to initiali ...