SPOJ - PERMJUMP Permutation Jumping
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 :
2
4 2
6 4
Sample Output :
3
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;
}
SPOJ - PERMJUMP Permutation Jumping的更多相关文章
- SPOJ 057 Supernumbers in a permutation
原题链接:http://www.spoj.com/problems/SUPPER/ 这道题n<=200000,那么确定为nlogn的算法,再定位到求LIS的O(nlogn)的算法. 对于每个a[ ...
- bzoj1318[spoj 744] Longest Permutation
题意 给出一个长度为n的,所有元素大小在[1,n]的整数数列,要求选出一个尽量长的区间使得区间内所有元素组成一个1到区间长度k的排列,输出k的最大值 n<=1e5 分析 不会做,好菜啊.jpg ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- xheditor的实例程序—类似word的编辑器
编辑器工具栏:类似word的编辑器 1.1.下载,兼容性 xhEditor官方网站地址为:http://xheditor.com/,打开右上角的免费下载 | 参数向导链接,即可找到最新版本的下载地址. ...
- Bootstrap历练实例:链接样式按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- shell脚本,判断给出的字符串是否相等。
第一种方法[root@localhost wyb]# .sh #!/bin/bash #判断给出的字符串是否相等 read -p "Please Input a number:" ...
- java在线聊天项目1.0版 异常处理——开启多个客户端,关闭一个客户端后,在其他客户端中再发出信息会出现异常的处理
异常一 只开启一个客户端,输入信息后关闭,客户端出现如下异常 根据异常说明 ChatClientFrame客户端117行 提示原因是Socket关闭 分析原因 客户端代码 while (connect ...
- HDU-1241-油藏
这题一道深搜的简单题目,其实题目的思路就只是向八个方向搜索,然后把整个油田遍历一遍即可. #include <cstdio> #include <cstring> int ma ...
- [JOYOI] 1035 棋盘覆盖
题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目描述 给出一张nn(n<=100)的国际象棋棋盘,其中被删除了一些点,问可以使用多 ...
- 如何用纯 CSS 创作阶梯文字特效
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MXYBEM 可交互视频教 ...
- linux-命令学习-1
1. cat命令 http://blog.csdn.net/jackalfly/article/details/7556848 cat主要有三大功能:1.一次显示整个文件.$ cat filena ...
- 关于学习Mongodb的几篇文章
一.Mongodb分片的使用 http://www.caiyiting.com/blog/2014/mongodb-sharding.html 二.MongoDB分布式高可用集群实现 http://w ...
- Python 前端 Html基础
概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页的标准语言.相当于定义统一 的规则.大家都来遵守它,这样就可以让浏览器根据标记语 ...