HDU-1799 (combination recurrence formula), hdu-1799 Recurrence Formula

Source: Internet
Author: User

HDU-1799 (combination recurrence formula), hdu-1799 Recurrence Formula
HDOJ-1799-Fighting_Dream

 

M-brute force solution and table Creation Time Limit:1000 MS Memory Limit:32768KB 64bit IO Format:% I64d & % I64u

Description

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.

Input

There are T group cases, T <= 10000. Each case has two integers, m and n, 0 <m <= 2000, 0 <n <=.

Output

For each case, output a value, indicating the total calculation amount. Maybe this number is large, you only need to output the remainder except 1007.

Sample Input

21 32 3

Sample Output

33

Question Analysis:

Note that the cyclic value is C (n, m) = n! /(N-m )! * 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). Once you know this problem, you can solve it.

Note:

Full Sorting Problem, C (n, m) = n! /(M! * (N-m )!)

However, considering that n is too large, this method cannot be used.
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;

AC code:

# Include <stdio. h> int c [2001] [2001]; void juge () {for (int I = 1; I <= 2000; I ++) {c [0] [0] = 1; c [I] [0] = 1; for (int j = 1; j <= 2000; j ++) c [I] [j] = (c [I-1] [j] + c [I-1] [j-1]) % 1007; // combination formula }}int main () {int T, n, m; juge (); scanf ("% d", & T); while (T --) {scanf ("% d", & m, & n); printf ("% d \ n", c [n] [m]) ;}}

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.