Discription

John likes playing the game Permutation Jumping. First he writes down a permutation A of the first n numbers. Then, he chooses any cell to start on. If he is currently at cell x and hasnt visited the cell A[x], he jumps to cell A[x]. He keeps doing this till he cannot move to the cell A[x], because he has already visited it. In the end, he counts all the cells that he visited during the game, including the cell on which he started. 
 
He does not want the game to go on for too long, and thus he wishes that irrespective of the choice of his starting cell, he does not ever have to visit more than K cells. On the other hand, he does not want the game to be too short either. Thus, irrespective of the choice of his starting cell, he should be able to visit atleast two cells. 
 
Now he wonders how many permutations could he have chosen in the first place which would allow him to have the game duration as above. i.e. He should visit atleast 2 cells and atmost K cells, no matter which cell he started on.

Input

The first line contains the number of test cases T (T <= 1000). The next T lines contain 2 space seperated integers N and K. (2 <= K <= N <= 100)

Output

Output T lines, one corresponding to each test case. For each test case output a single integer which is the answer for the corresponding test case. Since the answer can be very large, output the answer modulo 1000000007.

Example

Sample Input : 

4 2 
6 4 
 
Sample Output : 

145 
 
Note : 
For the first case, the valid permutations are {2 1 4 3}, {3 4 1 2} and {4 3 2 1}.

设f[i]为i的排列中满足条件的个数,转移的时候直接枚举1所在的循环的大小,再乘上其他数位置的排列数即可。

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

  

最新文章

  1. js计时器
  2. JS 内置对象
  3. 【BZOJ】1406: [AHOI2007]密码箱
  4. mysql的事务和select...for update
  5. ABBYY FineReader的图像编辑器功能使用方法
  6. visual studio F12 失效,可能是装了插件,比如ReSharper 但是,ReSharper没有激活导致.
  7. Debian 8 安装 Nvidia 显卡驱动
  8. RFID开发笔记 Alien阅读器文档
  9. 域名的MX设置及校验方法
  10. iOS UIWebView 访问https 绕过证书验证的方法
  11. Abstraction elimination
  12. spring学习(二) ———— AOP之AspectJ框架的使用
  13. vs与linux的交叉编译环境搭建
  14. [转帖] testin 安全测试要点
  15. WebRTC 配置环境
  16. Winfrom窗体无法关闭问题--检查是否存在重写
  17. flask文件上传
  18. PHP new StdClass() 创建空对象
  19. codeforces 388D Fox and Perfect Sets(线性基+数位dp)
  20. 洛谷P1514 引水入城

热门文章

  1. 【OS_Linux】Linux下软件的安装与卸载
  2. 细说unittest-2
  3. Lex与Yacc学习(十)之Yacc库
  4. JS(DOM 和 BOM)
  5. VisionPro工业视觉的标定方法
  6. Knockout v3.4.0 中文版教程-3-监控-通过监控创建视图模型(下)
  7. python基础-对象
  8. windows下在指定目录下打开命令行
  9. C#-dynamic参考
  10. Python之虚拟机操作:利用VIX二次开发,实现自己的pyvix(系列一)成果展示和python实例