"Problem description"
Little sin is in the class have n students, is ready to row into a column, but they do not want to height from the low to high row, so too monotonous, too no personality. They hope that there are just k pairs of classmates are high in front, short in the rear, the rest are short in front, high in the rear. If the n=5,k=3, if 5 people from low to high are labeled 1, 2, 3, 4, 5, then (1,5,2,3,4), (2,3,1,5,4), (3,1,4,2,5) are feasible row method. Little sin wants to know the total number of feasible methods.
Input
The input file name is lineup.in.
A row of two integers n and k, meaning the problem description.
Output
The output file name is Lineup.out.
Outputs an integer representing the number of feasible rows. Because the result can be large, output the value of the number of mod 1799999.
"Input and Output sample"
Lineup.in |
Lineup.out |
5 3 |
15 |
"Data Range"
For 20% of the data, there are n≤10,k≤40;
For 60% of the data, there are n≤100,k≤500;
For 100% of data, there is n≤100,k≤n* (n-1)/2.
This problem is not particularly difficult, in fact, only need to analyze this, assuming that the first classmate of the first (i-1) composed of a group of J ' pairs of the queue (assuming the size of the students joined from low to high)
, I do not add to the last side of the number of reverse pairs, but inserted in the K (from the back to the number of) students will be added in front of the K-reverse pairs (the first classmate height), as follows:
Original queue: 1 4 2 3
Insert the 5th classmate into the 2nd (k = 3) before the classmate: 1 5 4 2 3
The upper Brown is the number of reverse pairs produced with this number.
Then reaching a state (I,J) is a queue of individuals (I-1) that all can be obtained by inserting the first class of J in reverse order
So there's no time for that.
For example, the original queue: 1 2 3
Insert into the number of reverse order to increase to 4, to increase the number of reverse pairs of the largest, will be the 4th classmate added to the first team, add 3 reverse pairs, 0 + 3 < 4
So this is not possible.
This condition is also satisfied (assuming that the number of previous reverse pairs is k) K + i-1 > J
This recursion is obtained (the initial value of F is 0) (if the following table is negative or no tube)
1 1 1 1 1]
It seems that the time complexity is O (kn2) for 100 such a range or can be too
But obviously can continue to optimize, just use a prefix and can easily replace the third cycle, you can also simplify the above recursive
, there are many ways to eliminate the third cycle.
Code:
1#include <iostream>2#include <fstream>3 #defineModer 17999994 using namespacestd;5Ifstream Fin ("lineup.in");6Ofstream Fout ("Lineup.out");7 intf[101][5051];8 Long Longs[101][5051];9 intn,k;Ten intbuf; One voidINIT_DP () { A for(inti =2; I <= n;i++){ -f[i][0] =1; -s[i][0] =1; the } - } - voiddp () { -f[2][1] =1; +s[2][1] = s[2][0] +1; - for(inti =3; I <= n;i++){ +BUF = (I-1) * (I-2)/2; A for(intj =1; J < I; J + +){ atF[I][J] = s[i-1][min (J,BUF)]; -S[I][J] = (S[i][j-1] + f[i][j])%Moder; - } - for(intj = i; J <=i* (I-1)/2; j + +){ -F[I][J] = (S[i-1][min (BUF,J)]-s[i-1][j-i] + moder)%Moder; -S[I][J] = (S[i][j-1] + f[i][j])%Moder; in } - } to } + intMain () { -Fin>>n>>K; the INIT_DP (); * DP (); $fout<<F[n][k];Panax Notoginseng return 0; -}
[To] queue up