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 ...
随机推荐
- Bootstrap历练实例:分页状态
分页的状态 下面的实例演示了上表中所讨论的 class .disabled..active 的用法: <!DOCTYPE html><html><head>< ...
- iOS 优秀博客
中文 iOS/Mac 开发博客列表 GitHub 上排名前 100 的 Objective-C 项目简介 GitHub 上都有哪些值得关注学习的 iOS 开源项目? iOS开发系列文章(持续更新……) ...
- 学c++有感
第一次学习这么课程的时候,感觉课堂和教材的内容基本上都能接受和理解,但真正实际动手编写程序又觉得一片空白无从下手,可谓是“欲起平之恨无力.”一开始编写程序时,总是出现错误,从而产生了恐惧感,认为自己不 ...
- HUAWEI交换机配置telnet登录
Huawei交换机配置Telnet登录 一,交换机开启Telnet服务 <Huawei>system-view ...
- LeetCode(111) Minimum Depth of Binary Tree
题目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...
- ProxyHandler处理器__代理设置__自定义opener
ProxyHandler处理器(代理设置) 使用代理IP,这是爬虫/反爬虫的第二大招,通常也是最好用的. 很多网站会检测某一段时间某个IP的访问次数(通过流量统计,系统日志等),如果访问次数多的不像正 ...
- 163 AJAX
// 163 AJAX Tab // update 2006.10.18 // 增加鼠标延迟感应特性. // update 2006.10.8 // A 标签 href 属性将保持原有HTML功能.增 ...
- BNUOJ 6727 Bone Collector
Bone Collector Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...
- 循环神经网络与LSTM网络
循环神经网络与LSTM网络 循环神经网络RNN 循环神经网络广泛地应用在序列数据上面,如自然语言,语音和其他的序列数据上.序列数据是有很强的次序关系,比如自然语言.通过深度学习关于序列数据的算法要比两 ...
- Leetcode 363.矩形区域不超过k的最大数值和
矩形区域不超过k的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,-2,3]], ...