#P16583. [GKS 2016 #B] Sherlock and Permutation Sorting
[GKS 2016 #B] Sherlock and Permutation Sorting
题目描述
Sherlock and Watson have already been introduced to sorting in their computer programming course. Now, Watson has always been curious about parallel computing and wants to sort a permutation of the integers through by breaking it into chunks, sorting the chunks individually, and then concatenating them.
For a permutation , a chunk is a contiguous subarray of the permutation: i.e., a sequence of elements , for the elements at indexes and such that .
Watson wants to partition his permutation into an ordered list of one or more chunks, without changing the order that the elements are in, in such a way that each element of the permutation is in exactly one chunk, and all elements in a chunk are smaller than all elements in any later chunk. For example, for the permutation , these are the only four legal ways for Watson to break it into chunks: or or or . Watson is happiest when there are as many chunks as possible; we denote the maximum number of chunks for a permutation as . In this example, the maximum number of chunks is .
Watson wants to consider all permutations of the numbers through , and find the sum of squares of . Watson knows Sherlock might come in handy and comes to him for help, but Sherlock is as clueless as Watson and asks you for help. As the sum of squares can be large, please find it modulo .
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each test case consists of one line with two integers and .
输出格式
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the sum of squares of for all permutations of size , modulo .
3
1 2
2 4
3 7
Case #1: 1
Case #2: 1
Case #3: 6
提示
In Case 1, there is only one permutation. .
In Case 2, there are two permutations. . . .
In Case 3, there are six permutations. . . . . . . .
Limits
.
Small dataset (Test set 1 - Visible)
.
.
Large dataset (Test set 2 - Hidden)
.
.