2 Queuing
(Lineup.pas/.c/.cpp)
"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.
Problem Solving Report
Such a question, like this set of questions, is full of time to think. (Ay, as if I made out, but in fact, I even did not think of the example "Day, 15, forget it." "So ah, or to have patience, the examination room must be quiet down, do not be impatient , there are students with two hours to find the law, ultimately not a conscientious, all over." )
First, we need to find the law, and the law is not randomly found, we need to observe and compare the two groups of data and even more. So, open the table mode:
N\k 0 1 2 3 4 5 6
1 1 0 0 0 0 0 0
2 1 1 0 0 0 0 0
3 1 2 2 1 0 0 0
4 1 3 5 6 5 3 1
5 1 4 9 15 20 22 20 .....
Each n has a limit of the number of k, that is, the length L, if K>l is 0, L =0,1,3,6, and so on. l+=i-1; each time you add a number that is 1 larger than the previous maximum, as follows:
Last: 1 2 3 4 5
This time add a 6 There are 6 ways to add: in 5, in the four-in-five, in three or three, in between two or six, before 1;
There are 6 kinds of increasing possibilities: no impact, more than one group, more than two groups, more than three groups, more than four groups, more than five groups
Thus the equations can be listed:F[i][j] (number of I, Fetch J) =f[i-1][j]+f[i-1][j-1]+...+f[i-1][j-i+1]
(conversion):F[i][j-1]=f[i-1][j-1]+f[i-1][j-2]+...+f[i-1][j-i]
(Two-type subtraction):F[i][j]=f[i][j-1]+f[i-1][j]-f[i-1][j-i]
With the equation, the code is much simpler.
1#include <iostream>2#include <cstdio>3 using namespacestd;4 intn,k;5 intf[ the][5005];6 Const intinf=1799999;7 intMain ()8 {9Freopen ("lineup.in","R", stdin);TenFreopen ("Lineup.out","W", stdout); OneCin>>n>>K; A intL=0; - for(intI=1; i<=n;i++) - { thel+=i-1; -f[i][0]=1; - for(intj=1; j<=k;j++) - { + if(j>l) Break; -F[i][j]= (f[i-1][j]+f[i][j-1])%inf; + if(j-i>=0) f[i][j]= (f[i][j]-f[i-1][j-i]+inf)%inf; A } at } -cout<<F[n][k]; - return 0; -}
Magical NOIP Simulation Test 2 line up