poj 3761 反逆序数+快速取模+阶乘预处理

本文探讨了一种基于冒泡排序的算法竞赛题目,通过对题目的深入分析,提出了解决方案并给出了具体的实现代码。文章重点介绍了如何通过计算反逆序数来确定初始数组的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bubble Sort
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2724 Accepted: 927

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 exactlyK 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 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 ≤ 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

3
3 0
3 1
3 2

Sample Output

1
3
2

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.



注意:

这里我们可以学到:

由于案例数量极大,因此我们可以对阶乘进行预处理,这样处理的时间虽然多,但是相对于那么多的案例来说已经很小了

但是阶乘时候数字会变得很大,所以在阶乘的时候就要进行取模

然后利用公式的取模的时候,一定要考虑为负数的情况,尤其是当有减号的时候

这个时候就要进行加一个取模数字,变成正数的时候再取模





题意:

冒泡排序的升级版,看题目中的 Hint 就可以看懂是什么意思了



题解:

这里需要构造一个反逆序数

对于排列  { B(j)}       :5,9,1,8,2,6,4,7,3

反逆序数的定义为  对于 B (j) 为位于j的左边但是大于 j 的元素的个数

例如首元素 5 ,下标为 1 ,那么 B 1==2,

就是    2,3,6,4,0,2,2,1,0        


根据规律可以知道,扫描的次数就是 max = { b j  };


任意给一个数字 k,反序表每一位上面的反序数取值范围 【0 ,n-k】

然后就可知道反序表为 【0 ,n-k】-【0 ,n-k-1】就是反序表为 k的

然后就可以有

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




#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define LL long long
#define MAXN 1000010
LL mmod=20100713;

LL num[MAXN];
void init()
{
    num[0]=1;
    for(int i=1;i<=MAXN;i++)
        num[i]=num[i-1]*i%mmod;
}

LL quickmod(int a,int n)
{
    if(n==0)
        return 1;
    LL ans=1;
    LL temp=a;
    while(n)
    {
        if(n&1)
            ans=ans*temp%mmod;
        temp=temp*temp%mmod;
        n>>=1;
    }
    return ans;
}

LL deal(int n,int k)
{
    LL ans=num[k]*(((quickmod(k+1,n-k)-quickmod(k,n-k))+mmod)%mmod);
    ans=ans%mmod;
    return ans;
}

int main()
{
    int T;
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    init();
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        printf("%lld\n",deal(n,m));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值