poj3761 Bubble Sort(排列组合,逆序数,乘方取余)
来源:互联网 发布:福利社区软件 编辑:程序博客网 时间:2024/05/17 01:58
Description
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.
Input
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 ≤ K ≤ N - 1) where N is the size of array and K is the number of Bubble Sort Rounds.
Output
For each line, output an integer which is the number of initial arrays mod 20100713.
Sample Input
33 03 13 2
Sample Output
132
Hint
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.
千万注意数据类型混用带来的麻烦
#include<stdio.h>#define M 20100713__int64 kk[1000004]= {1};__int64 abm(__int64 a,__int64 b,__int64 m){ __int64 ans=1; while(b) { if(b&1) ans=ans*a%m; a=a*a%m; b>>=1; } return ans;}int main(){ int T,i; __int64 n,k,tmp; for(i=1; i<1000004; i++) kk[i]=kk[i-1]*i%M; scanf("%d",&T); while(T--) { scanf("%I64d%I64d",&n,&k); tmp=abm(k+1,n-k,M)-abm(k,n-k,M); printf("%I64d\n", (kk[k]%M*((tmp+M)%M))%M); } return 0;}
- poj3761 Bubble Sort(排列组合,逆序数,乘方取余)
- poj3761 bubble sort
- POJ3761 Bubble Sort
- POJ 3761 Bubble Sort (乘方取模)
- HDU 5775 (Bubble Sort 逆序数)
- POJ3761阶乘取余问题
- hdu5775 Bubble Sort(线段树求逆序数)
- HDU 5775 Bubble Sort(归并排序+逆序数)
- 【反序表】POJ3761[Bubble Sort]题解
- [数学杂题] POJ3761 Bubble Sort
- HDU 5775 Bubble Sort(逆序对)
- HDU5775-Bubble Sort(逆序对)
- hdu5775 Bubble Sort 树状数组维护逆序数
- HDU 5775 Bubble Sort 树状数组求逆序数
- 【归并排序】【逆序数】HDU 5775 Bubble Sort
- poj 3761 bubble sort#排列组合
- poj 3761 bubble sort (排列组合)
- POJ1995 Raising Modulo Numbers(乘方取余)
- Java基础入学考blog-5
- UVA 10970 (暑假-数学(3) -E - Big Chocolate)
- linux nohup命令详解
- Map集合
- extjs各种弹出框
- poj3761 Bubble Sort(排列组合,逆序数,乘方取余)
- AC自动机
- 从Spring For Android开始对Android的研究(一)——一个完整的例子
- UVA537
- POJ3761
- Linux下errno所代表的含义
- poj 1837
- UVA 10014(暑假-数学(3) -F - Simple calculations)
- hdu 4857 逆向拓扑排序+反向输出