Question meaning:
We know that in programming, we often need to consider the time complexity, especially for the loop. For example,
If
For (I = 1; I <= N; I ++) OP;
N op operations are performed.
Fori = 1; I <= N; I ++)
For (j = I + 1; j <= N; j ++) OP;
Then, N * (n-1)/2 op operations are performed.
Now we know that there are m-layer for loop operations, and the starting value of each for variable is the starting value of the previous variable + 1 (the starting value of the first variable is 1 ), the end value is an input N, and the total calculation amount of the op is asked.
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1799
Question Analysis:
Note that the cyclic value is C (n, m) = n! /(M! * (N-m )!), The formula cannot be used directly because the N value is too large.
Recursive Formula for composite mathematics: C (n, m) = C (n, m) + C (N-1 m-1)
AC code:
/**
* Full Sorting Problem, C (n, m) = n! /(M! * (N-m )!)
* This method cannot be used because the N value is too large.
* The combination formula C (n, m) = C (n-1, m) + C (N-1 m-1) can be used)
* C (n, 1) = N; C (n, n) = 1; C (n, 0) = 1;
* In this way, you can use DP.
*/
# Include <iostream>
# Include <algorithm>
Using namespace STD;
Int DP [2001] [2001];
Int main ()
{
For (INT I = 0; I <= 2000; I ++ ){
DP [I] [1] = I %1007; // C (n, 1) = N;
DP [I] [0] = DP [I] [I] = 1; // C (n, 0) = C (n, n) = 1;
}
For (INT I = 2; I <= 2000; I ++ ){
For (Int J = 1; j <= I; j ++ ){
DP [I] [J] = (DP [I-1] [J] + dp [I-1] [J-1]) % 1007;
}
}
Int t, n, m;
Cin> T;
While (t --){
Cin> m> N;
Cout <DP [N] [m] <Endl;
}
Return 0;
}
Hdu1799 (calculate the number of combinations using Recursive formulas)