题目链接:http://codeforces.com/gym/101955/problem/C
Time limit:6.0 s Memory limit:1024 MB
Problem Description
Insertion sort is a simple sorting algorithm that builds the final sorted array one item at an iteration.
More precisely, insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
This type of sorting is typically done in-place, by iterating up the array, growing the sorted array behind it. At each array-position, it checks the value there against the largest value in the sorted array (which happens to be next to it, in the previous array-position checked). If larger, it leaves the element in place and moves to the next. If smaller, it finds the correct position within the sorted array, shifts all the larger values up to make a space, and inserts into that correct position.
The resulting array after k iterations has the property where the first k k k entries are sorted. In each iteration the first remaining entry of the input is removed, and inserted into the result at the correct position, thus extending the result.
Knuth is an ACM-ICPC master and provides a modified pseudocode implementation about the insertion sort for you. His modified algorithm for an array of sortable items
A
A
A (1-based array) can be expressed as:
He notes that a permutation of
1
1
1 to
n
n
n is almost sorted if the length of its longest increasing subsequence is at least
(
n
−
1
)
(n−1)
(n−1).
Given the parameter k, you are asked to count the number of distinct permutations of 1 to n meeting the condition that, after his modified insertion sort, each permutation would become an almost sorted permutation.
Input
The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 5000 5000 5000.
For each test case, the only line contains three integers n n n, k k k and q q q indicating the length of the permutations, the parameter in his implementation and a prime number required for the output respectively, where 1 ≤ n , k ≤ 50 1≤n,k≤50 1≤n,k≤50 and 1 0 8 ≤ q ≤ 1 0 9 10^8≤q≤10^9 108≤q≤109.
Output
For each test case, output a line containing “Case #x: y” (without quotes), where x x x is the test case number starting from 1 1 1, and y y y is the remainder of the number of permutations which meet the requirement divided by q q q.
Example
Input
4
4 1 998244353
4 2 998244353
4 3 998244353
4 4 998244353
Output
Case #1: 10
Case #2: 14
Case #3: 24
Case #4: 24
Note
In the first sample case, we can discover 10 permutations which meet the condition, and they are listed as follows:
[1,2,3,4];
[1,2,4,3];
[1,3,2,4];
[1,3,4,2];
[1,4,2,3];
[2,1,3,4];
[2,3,1,4];
[2,3,4,1];
[3,1,2,4];
[4,1,2,3].
Problem solving report:
Description:求
1
∼
n
1 \sim n
1∼n有多少个排列使得前k个有序之后最长上升子序列大于等于n-1。
Problem solving:打表找规律。
先上打表Code:
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 30;
int nn, k;
int a[MAXN], b[MAXN], dp[MAXN];
int deal(int n) {
for (int i = 1; i <= n; i++)
dp[i] = 1;
int ret = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (b[j] < b[i] && dp[j] + 1 > dp[i])
dp[i] = dp[j] + 1;
}
if (dp[i] > ret)
ret = dp[i];
}
return ret;
}
int main() {
nn = 10;
int fac[15] = {0, 1};
for (int i = 2; i <= nn; i++)
fac[i] = fac[i - 1] * i;
for (int n = 1; n <= nn; n++) {
printf("[%d]\t", n);
for (int i = 1; i <= n; i++)
a[i] = i;
for (int k = 1; k <= n; k++) {
int cnt = 0;
do {
memcpy(b, a, sizeof(a));
sort(b + 1, b + 1 + k);
if (deal(n) >= n - 1)
cnt++;
} while (next_permutation(a + 1, a + n + 1));
printf("%d %d\t", fac[k], cnt);
}
printf("\n");
}
return 0;
}
通过打表很容易可以发现,
a
n
s
/
k
!
ans/k!
ans/k!是一个等差数列。其中
a
n
s
ans
ans为最后的未取模的结
果。
例如:
n = 4: ans 10 14 24 24 k! 1 2 6 24 ans / k! 10 7 4 1 首项:10 = (n - 1) * (n - 1) + 1, 公差为:3 = n - 1. n = 5: ans 17 26 54 120 120 k! 1 2 6 24 120 ans / k! 17 13 9 5 1 首项:17 = (n - 1) * (n - 1) + 1, 公差为:4 = n - 1. ......
故,有首项、有公差,即可求出答案。
Accepted Code:
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, kase = 0;
scanf("%d", &t);
while (t--) {
int n, k, q;
scanf("%d%d%d", &n, &k, &q);
k = min(n, k);
int m = (n - 1) * (n - 1) + 1;
long long fact = 1;
for (int i = 2; i <= k; i++) {
m -= (n - 1);
fact = fact * i % q;
}
printf("Case #%d: %lld\n", ++kase, fact * m % q);
}
return 0;
}