51NOD1020 reverse order (DP)

Source: Internet
Author: User

1020 reverse permutation base time limit: 2 seconds Space limit: 131072 KB score: 80 Difficulty: 5-level algorithm topic collection focus on a permutation, if the number of the front and back position of the opposite of the size order, that is, the previous number is greater than the next number, then they are called a reverse order. The total number of reverse order in a permutation is called the inverse number of the permutation. such as 2 4 3 1, 2 1,4 3,4 1,3 1 is reverse, the reverse number is 4.
1-n in the whole arrangement, the number of reverse order is 0 (positive), the maximum is n (n-1)/2 (reverse) given 2 numbers n and K, for the 1-n of the whole arrangement, the number of reverse order is k of how many kinds? For example: n = 4 K = 3.
1 2 3 4 A total of 3 in reverse order 6, respectively: 1 4 3 22 3 4 12 4 1 33 1 4 23 2 1 44 1 2 3
Since the number of reverse permutations is very large, it is only possible to calculate and output the number of Mod 10^9 + 7 results. Input
Line 1th: A number t that represents the number of numbers that are later used as input tests. (1 <= T <= 10000) 2-t + 1 lines: 2 numbers per line n,k. The middle is separated by a space. (2 <= N <=, 0 <= k <= 20000)
Output
Total T-line, corresponding to the number of reverse order Mod (10^9 + 7)
Input example
14 3
Output example
6


Set F (n,k) indicates the number of permutations in the order of N of the number of numbers in reverse order of K.

The largest number of n may be ranked in the first n-i, resulting in an I and N-related reverse order, minus N, the remaining number of n-1 is k-i in reverse order. So, f (n,k) = SUM (f (n-1,k-i)) (0<=i<n). Similarly there is f (n,k-1) = SUM (f (n-1,k-1-i)) (0<=i<n). Subtract from the two, you can get F (n,k)-F (n,k-1) =f (N-1,k)-F (n-1,k-n). The recursive formula is F (n,k) =f (n,k-1) +f (N-1,k)-F (n-1,k-n).



#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string > #include <functional> #include <cmath> #include <set> #include <queue> #include <  algorithm> #include <vector> #include <map> #include <stack>using namespace std; #define ESP 1e-8const Double PI = ACOs ( -1.0); const double e = 2.718281828459;const int inf = 2147483647;const long Long mod = 10000000 07;typedef Long Long Ll;//freopen ("In.txt", "R", stdin); input redirection, input data will be read from the In.txt file//freopen ("OUT.txt", "w", stdout);  Output redirection, the output data will be saved in the OUT.txt file int dp[1005][20005];void init () {for (int i = 1; I <=, ++i) dp[i][0] = 1;for (int i = 2; I <= 1000; ++i) {for (int j = 1; J <= I * (i-1)/2 && J <= 20000; ++j) {Dp[i][j] = (Dp[i][j-1] + dp[i-1][j])% mod if (j-i >= 0) dp[i][j] = dp[i-1][j-i];DP [i][j] = (dp[i][j]% mod + mod)% mod;}} int main () {int T, N, K;init (), scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &k);p rintf ("%d\n", Dp[n][k]);}} 



51NOD1020 reverse order (DP)

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.