POJ 3761 Bubble Sort: Analysis of permutations by inverse sequence table

Source: Internet
Author: User
Tags range sort

http://poj.org/problem?id=3761

1. First, introduce the concept of the reverse order table:

To make Bi (1<=i<=n) the number of elements that are located on the left side of I but greater than I, we can get the inverse sequence table b1,b2,..., of the permutation a1,a2,..., an B3

For example, arrange

5 9 1 8 2 6 4 7 3

have reverse order table

2 3 6 4 0 2 2 1 0 (1 on the left and greater than 1, 2 on the left and greater than 2, ...) )

2. Key conclusions:

By 1, the inverse ordinal value range of the 1th element is [0,n-1], the inverse ordinal value range of the I element is [0,n-i], and the inverse ordinal of the last element can only be 0

(Note that each inverse ordinal can be arbitrarily taken in the interval without considering the value of other inverse ordinals, i.e. the inverse ordinal is independent of each other.) The reason is that the number of the reverse sequence table is also n!, and the permutation and the reverse sequence table are one by one corresponding relations. )

3.

It's not hard to find that every trip bubble sort will subtract 1 from the inverse ordinal of an element with an inverse ordinal greater than 0. If the order is arranged after K-trip, the maximum value in the inverse sequence table is k.

Thus, the title can be converted to the maximum value of k in the sequence of the known n elements, and the number of such permutations.

4. The Reverse ordinal table number that does not exceed K is now to be calculated:

Because the inverse ordinal number does not exceed K, then when the n-i<=k, that is, i>=n-k, the element i is free to put. Because no matter how, their inverse ordinal numbers are not greater than k, the number of values determined by I, so the reverse sequence table has pi (n-i+1) = (k+1)! species (N-k<=i<=n).

When I<n-k, because the inverse ordinal number does not exceed K, the inverse sequence of these elements can only be selected between [0,k], so the reverse sequence table has (k+1) ^ (n-k-1) species.

By the multiplication theorem, the inverse sequence table number is k!* (k+1) ^ (n-k).

5. But we have to ensure that there is an element of the inverse ordinal number k, how to do?

It is very simple to calculate the inverse ordinal number that does not exceed k-1, and subtract from the result of 4, which is the final result.

ans=k!* (k+1) ^ (n-k)-(k-1)!* (k) ^ (n-k+1) =k!* ((k + 1) ^ (n-k)-k^ (n-k))

6. In programming, first preprocessing factorial and then using fast power to calculate the power part.

Complete code:

[Cpp]view plaincopyprint?

/*454ms,7984kb*/

#include <cstdio>

typedef __int64 LL;

CONST LL mod = 20100713LL;

const int MAXN = 1000001;

ll FACTORIAL[MAXN] = {1LL};

inline ll Mpow (ll a, int n)

{

ll ans = 1LL;

while (n)

{

if (n & 1) ans = (ans * a)% mod;

A = (A * a)% mod;

n >>= 1;

}

return ans;

}

int main ()

{

int T, n, K;

ll ans;

for (int i = 1; i < MAXN; ++i)

Factorial[i] = (factorial[i-1] * i)% mod;

scanf ("%d", &t);

while (t--)

{

scanf ("%d%d", &n, &k);

printf ("%i64d\n", Factorial[k] * (Mpow (k + 1, n-k)-Mpow (k, n-k) + MoD)% mod);

}

return 0;

}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.