Reverse number of columns [HAOI2009]

[Title Description
for a number of columns \ (A \) , if there \ (i <j \) and \ (a_i> a_j \) , then we say \ (a_i \) and \ (a_j \) as a pair of reverse order number. For if any one of (1 \ sim n \) \ series consisting of natural numbers, it is easy to count the number of reverse has obtained. Then the reverse of the number \ (k \) in the end how many such natural numbers the number of columns?

[Input format
of the first two acts of integers \ (n-, K \) .

[Output Format]
write an integer representing the number of qualifying series since this number may be large, you only need to output the number of \ (\ 10000) results after seeking the remainder.

answer

Emergent it. . . Simple push DP equation

\ (dp [i] [j ] \) represents \ (1 \ sim i \) a reverse arrangement to the full number \ (J \) is the number of

Border: \ (DP [2] [0] = DP [2] [. 1] =. 1 \) special attention \ (dp [1] [0 ] = 1 \)

Here is the transfer equation

For chestnut it: \ (3,1,2 \) Now the \ (4 \) added to the list if the increase of the \ (K \) behind the number \ ((0 \ le k \ le 3) \) reverse order it will be more \ (3-k \) th

Then it bin \ (dp [i] [j ] = \ sum \ limits_ {k = 0} ^ {i-1} dp [i-1] [jk] \)

Like on push \ (DP [4] [3] \) it \ (4 \) in a first array, and the number of reverse \ (3 \) number scheme is \ (dp [3] [0 ] \ ) because you \ (4 \) put the first one will generate more than \ (3 \) inversions of thing that the original \ (3 \) number can only reverse is not right

Prefix and \ (O (1) \) transfer

\ (dp \) array dimension can be removed first and then do the offline data, although this is also nothing but water does not get rid of one year ACM / ICPC seems exam, but the original title card space

Then off to a query sequence from small to large storage side edge DP answer time complexity \ (O (n ^ 2) \)

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') f = -1;
    for (; ch <= '9' && ch >= '0'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ '0');
    return x * f;
}

const ll mod = 1000000007;
int tot, now;
ll dp[5005], sum[5005], ans[5005];

struct question{
    int n, m, ind;
} q[5005];

inline bool cmp(question a, question b) {
    return a.n < b.n;
}

inline void DP() {
    dp[0] = dp[1] = 1;
    for (ll j = 0; j <= 5000; j++) {
        sum[j] = j == 0 ? 1 : 2;
    } 
    while (now <= tot && q[now].n == 2) {
        ans[q[now].ind] = dp[q[now].m];
        now++;
    }
    for (ll i = 3; i <= 5000; i++) {
        memset(dp, 0, sizeof(dp));
        for (ll j = 0; j <= min(i * (i - 1) / 2, 5000ll); j++) {
            if (j - i < 0) {
                dp[j] = sum[j];
            } else {
                dp[j] = (sum[j] - sum[j - i] + mod) % mod;
            }
        }
        memset(sum, 0, sizeof(sum));
        sum[0] = dp[0];
        for (ll j = 1; j <= 5000; j++) {
            sum[j] = (sum[j-1] + dp[j]) % mod;
        }
        while (now <= tot && q[now].n == i) {
            ans[q[now].ind] = dp[q[now].m];
            now++;
        }
    }   
}

int main() {
    tot = read();
    for (int i = 1, n, m; i <= tot; i++) {
        n = read(); m = read(); 
        q[i] = question{n, m, i};
    }
    sort(q + 1, q + tot + 1, cmp);
    now = 1; 
    while (now <= tot && q[now].n == 1) {
        ans[q[now].ind] = q[now].m == 0 ? 1 : 0;
        now++;
    }
    DP();
    for (int i = 1; i <= tot; i++) {
        printf("%lld\n", ans[i]);
    }
    return 0;
} 

Guess you like

Origin www.cnblogs.com/ak-dream/p/AK_DREAM53.html