The number of the original sequence of bubbles sorted by inverse request

Source: Internet
Author: User
Tags arrays rounds sort first row

Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to being sorted, comparing each pair of adjacent items and swapping them if They is in the wrong order. The pass through the list is repeated until no swaps be needed, which indicates the list is sorted. The algorithm gets its name from the smaller elements "bubble" to the top of the list. Because it uses comparisons to operate on elements, it's a comparison sort.
Wikipedia

Bubble Sort is a very simple sorting algorithm which runs in O (N2) time. Each round, we start from the beginning of the list, compare each adjacent pair of items in turn, swapping the items if NE Cessary. Repeat the pass through the list, until no swaps is done. Assume that after exactly T rounds, the array was already in the ascending order, then we say that T is the number of BUBBL E Sort Rounds of this array. Below is a example:let us take a array of numbers "5 1 4 2 8", then we sort the array using Bubble sort as follow:

First Round:
(5 1 4 2 8)--(1 5 4 2 8), compares the first and elements, and swaps them.
(1 5 4 2 8)-(1 4 5 2 8), Swap since 5 > 4
(1 4 5 2 8)-(1 4 2 5 8), Swap since 5 > 2
(1 4 2 5 8)--(1 4 2 5 8), since these elements is already in order (8 > 5), algorithm does not swap them.
Second Round:
(1 4 2 5 8)--(1 4 2 5 8)
(1 4 2 5 8)-(1 2 4 5 8), Swap since 4 > 2
(1 2 4 5 8)--(1 2 4 5 8)
(1 2 4 5 8)--(1 2 4 5 8)

After T = 2 rounds, the array was already sorted, hence we say that the number of Bubble Sort rounds of this array is equal to 2.

ZX learns Bubble Sort in an algorithm class and his teacher leaves him a problem as homework. The teacher gives ZX an array A with N distinct numbers which are already sorted in ascending order and he tells ZX that th Is array was obtained after exactly K rounds of Bubble sort. The problem is:how Many initial arrays there may is from which we can obtain the array A after exactly K rounds of Bubble Sort? The result may be very large and so are need to output the answer mod 20100713.

Input
The input may contain several cases.
The first line contains an integer T (t≤100,000), indicating the number of test cases.
Then T lines of test cases follow. For each line, it contains-integers N and K (1≤n≤1,000,000, 0≤k≤n-1) where N is the size of an array and K is T He number of Bubble Sort Rounds.
Output
For each line, output a integer which is the number of initial arrays mod 20100713.

Sample Input
3
3 0
3 1
3 2
Sample Output
1
3
2
Hint
Suppose the order Ed Array is {A, B, C} (a < b < c). For the 6 possible initial arrays:
{A, B, C}, {A, C, b}, {B, A, c}, {B, C, a}, {C, a, b}, {C, B, a},
we can get That:
{A, B, C}: Already sorted, no bubble sort needed.
{A, C, b}, {B, A, c}, {C, a, B}: We need 1 round of Bubble Sort.
{b, C, a}, {C, B, a}: We need 2 rounds of Bubble Sort.

Idea:
for the bubble sort, go to the array of one of the number in reverse order to the most minus one, then only if there is no reverse order in the array of time will stop, so the main point is to find the n number of the largest reverse order for the arrangement of K, and the direct request is not very good, I turn to the number of n in the inverse of the array of K, only for the number of n-k is likely to be the largest inverse of the K, so the first row of these numbers each number has k+1, the remaining K number of the entire row, namely k!* (k+1) ^ (n-k); ((k+1) ^ (n-k)-k^ ( K-N)) k! The result of the request;

 #include <cstdio> #include <math.h> using namespace std; const int mod=20100713;
Long Long ff[1000090];
    A long long cal (int a,int b) {long long t=a,ans=1;
        while (b) {if (b&1) ans= (ans*t)%mod;
        T=t*t%mod;
    b>>=1;
} return ans;
    } void F () {ff[0]=1;
for (int i=1;i<=1000001;i++) Ff[i]=ff[i-1]*i%mod;
    } int main () {int t;
    scanf ("%d", &t);
    f ();
        while (t--) {int n,k;
        scanf ("%d%d", &n,&k);
    printf ("%lld\n", (Cal (K+1,n-k)-cal (k,n-k) +mod) *ff[k]%mod);
} return 0; }

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.