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 ...
随机推荐
- Swift 编程思想 Part 4:map all the things!
Swift 编程思想 Part 4:map all the things! 2015-10-22 837 文章目录 1. 数组 vs. 可选类型 2. 作用在可选类型上的 map() 3. 回到我们 ...
- 智能指针之 weak_ptr
1. weak_ptr 介绍 std::weak_ptr 是一种智能指针,它对被 std::shared_ptr 管理的对象存在非拥有性("弱")引用.在访问所引用的对象指针前必须 ...
- 【chm】【windows】win7下chm打开不显示内容
修改chm属性里面,‘解除锁定’即可.点击chm文件,右键选择属性,点击最下方的解除锁定,保存,退出重新打开即可.
- 细说unittest-1
转自:https://www.jianshu.com/p/6c07be6d61dc 一.什么是unittest unittest是Python单元测试框架,类似于JUnit框架. unittest中有 ...
- vfs_caches_init函数解析
vfs_caches_init函数初始化VFS,下面梳理函数调用流程 start_kernel() -->vfs_caches_init_early(); -->dcache_init_e ...
- H.264 与 MPEG-4 压缩格式的变革
h.264 和 mpeg-4 的关系: h.264 /avc ( advanced video coding )标准,是 mpeg-4 的第 10 部分. mpeg-4的初衷是将dvd质量的图像码流从 ...
- MPEG-4视频编码核心思想
1 引言 当今时代,信息技术和计算机互联网飞速发展,在此背景下,多媒体信息已成为人类获取信息的最主要载体,同时也成为电子信息领域技术开发和研究的热点.多媒体信息经数字化处理后具有易于加密.抗干扰能 ...
- H.264基本原理与编码流程
H264视频压缩算法现在无疑是所有视频压缩技术中使用最广泛,最流行的.随着 x264/openh264以及ffmpeg等开源库的推出,大多数使用者无需再对H264的细节做过多的研究,这大降低了人们使用 ...
- DocView mode 1 -- 手册翻译
文档原文在线地址 * 35 Document Viewing** DocView mode is a major mode for viewing DVI, PostScript (PS), PDF, ...
- AVL树总结
定义:一棵AVL树或者是空树,或者是具有下列性质的二叉搜索树:它的左子树和右子树都是AVL树,且左右子树的高度之差的绝对值不超过1 AVL树失衡旋转总结: 假如以T为根的子树失衡.定义平衡因子为 H( ...