当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

3761:Bubble Sort

题目描述

Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is 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 necessary. Repeat the pass through the list, until no swaps are done. Assume that after exactly T rounds, the array is already in the ascending order, then we say that T is the number of Bubble Sort Rounds of this array. Below is an example: Let us take an 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 two 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 are 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 is 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 is already sorted in ascending order and he tells ZX that this array is obtained after exactly K rounds of Bubble sort. The problem is: How many initial arrays there may be from which we can obtain the array A after exactly K rounds of Bubble Sort? The result may be very large, so you only need to output the answer mod 20100713.  

输入解释
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 two integers N and K (1 ≤ N ≤ 1,000,000, 0 ≤ KN - 1) where N is the size of array and K is the number of Bubble Sort Rounds.
输出解释

For each line, output an integer which is the number of initial arrays mod 20100713.

输入样例
3
3 0
3 1
3 2
输出样例
1
3
2
提示

Suppose the ordered 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.
 


该题目是Virtual Judge题目,来自 北京大学POJ

源链接: POJ-3761

最后修改于 2020-10-29T07:10:47+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536