题面

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. ­­­­­­­­­­——­­­­­­­­­­W­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­i­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­k­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­i­­­­­­­­­­­­­­­­­­­­p­­­­­­­­­­­­­­­­­­­­e­­­­­­­­­­­­­­­­­­­­d­­­­­­­­­­­­­­­­­­­­i­­­­­­­­­­­­­­­­­­­­a­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

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 2­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­0­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­1­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­0­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­0­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­7­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­1­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­3.

题解

是一道结论+构造的题。

对于冒泡排序,相信大家都已经很­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­熟­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­悉­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­了­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­。它除了逆序对的性质以外,还有一个性质:每一轮排序,都会使得每个数前面比它大的数个数减少 1,减到 0 不变。

因此,我们可以令

d

i

d_i

di​ 为数字

i

i

i 前面比它大的数个数,那么有

i

,

0

d

i

n

i

\forall i,0\leq d_i\leq n-i

∀i,0≤di​≤n−i 。

为了保证

K

K

K 轮后恰好排完,我们得使

max

d

i

=

K

\max d_i=K

maxdi​=K 。

所以,合法的

d

d

d 序列就有

K

!

(

(

K

+

1

)

n

k

K

n

k

)

K!\cdot((K+1)^{n-k}-K^{n-k})

K!⋅((K+1)n−k−Kn−k) 个。

事实是,这个即为答案,因为我们可以通过一个

d

d

d 序列构造出一个唯一的排列:

  • 从大到小往排列中放入数字。
  • 恒有

    d

    n

    =

    0

    d_n=0

    dn​=0 ,先放入数字

    n

    n

    n ,然后

    d

    n

    1

    {

    0

    ,

    1

    }

    d_{n-1}\in\{0,1\}

    dn−1​∈{0,1} ,为

    0

    0

    0 就把

    n

    1

    n-1

    n−1 紧挨着放

    n

    n

    n 的左边,否则放右边,此时暂时得到一个长为 2 的序列,继续……

  • 我们放数字

    i

    i

    i 时,将

    i

    i

    i 插入到当前已形成的序列第

    d

    i

    d_i

    di​ 个位置的后面(为 0 则放最开头),使之满足

    d

    i

    d_i

    di​ 的限制。而且,有且仅有这种放法是满足限制的。

最终放完所有数字后,形成一个长度为

n

n

n 的排列,就是一种方案。

综上,答案就是

K

!

(

(

K

+

1

)

n

k

K

n

k

)

K!\cdot((K+1)^{n-k}-K^{n-k})

K!⋅((K+1)n−k−Kn−k) ,预处理阶乘+快速幂 可以达到

O

(

T

log

n

+

n

)

O(T\log n+n)

O(Tlogn+n) 。

CODE

#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 1000005
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x) & (x))
LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s=='-')f = -f;s = getchar();}
while(s >= '0' && s <= '9') {x=x*10+(s-'0');s = getchar();}
return f * x;
}
const ­­­­­­­­­­­­­­­­­­­­int MOD = 20100713;
int n,m,i,j,s,o,k;
int fac[MAXN];
int qkpow(int a,int b) {
int res = 1;
while(b > 0) {
if(b & 1) res = re­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­s *1ll* a % MOD;
a = a *1ll* a % MOD; b >>= 1;
}return res;
}
char ss[MAXN];
int main() {
fac[0] = fac[1] = 1;
for(int i = 2;i <= 100­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­0000;i ++) {
fac[i] = fac[i-1]*1ll*i % MOD;
}
int T = read();
while(T­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ --) {
n = read();k = read();
int ans = (qkpow(k+1,n-k)+MOD-qkpow(k,n-k)) % MOD *1ll* fac[k] % MOD;
printf("%d\n",ans);
}
return 0;
}

POJ3761 Bubble Sort (组合数学,构造)的更多相关文章

  1. POJ3761 Bubble Sort

    对1~n组成的序列进行冒泡排序,一共进行了k趟,问有几个符合题意的序列. 注意:这里指每一趟是指交换当前相邻的全部逆序对,比如:2 1 4 3进行一趟交换就是1 2 3 4 假设我们细心观察.就会发现 ...

  2. POJ 3761:Bubble Sort——组合数学

    题目大意:众所周知冒泡排序算法多数情况下不能只扫描一遍就结束排序,而是要扫描好几遍.现在你的任务是求1~N的排列中,需要扫描K遍才能排好序的数列的个数模20100713.注意,不同于真正的冒泡排序算法 ...

  3. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  4. Bubble Sort (5775)

    Bubble Sort Problem Description   P is a permutation of the integers from 1 to N(index starting from ...

  5. Bubble Sort [ASM-MIPS]

    # Program: Bubble sort # Language: MIPS Assembly (32-bit) # Arguments: 5 unordered numbers stored in ...

  6. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...

  8. 快速幂取模 POJ 3761 bubble sort

    题目传送门 /* 题意:求冒泡排序扫描k次能排好序的全排列个数 数学:这里有一个反序列表的概念,bj表示在j左边,但大于j的个数.不多说了,我也是看网上的解题报告. 详细解释:http://blog. ...

  9. 冒泡排序(Bubble Sort)

    常见的排序算法有Bubble Sort.Merge Sort.Quick Sort 等,所有排序算的基本法思想都是把一个无限大的数据规模通过算法一步步缩小,指导最后完成排序. 这里分享一下Buuble ...

随机推荐

  1. Spring cloud gateway 如何在路由时进行负载均衡

    本文为博主原创,转载请注明出处: 1.spring cloud gateway 配置路由 在网关模块的配置文件中配置路由: spring: cloud: gateway: routes: - id: ...

  2. Docker安装Jenkins打包Maven项目为Docker镜像并运行【保姆级图文教学】

    一.前言 Jenkins作为CI.CD的先驱者,虽然现在的风头没有Gitlab强了,但是还是老当益壮,很多中小公司还是使用比较广泛的.最近小编经历了一次Jenkins发包,感觉还不错,所以自己学习了一 ...

  3. 李呈祥:bilibili在湖仓一体查询加速上的实践与探索

    导读: 本文主要介绍哔哩哔哩在数据湖与数据仓库一体架构下,探索查询加速以及索引增强的一些实践.主要内容包括: 什么是湖仓一体架构 哔哩哔哩目前的湖仓一体架构 湖仓一体架构下,数据的排序组织优化 湖仓一 ...

  4. WPF第三方控件,只能输入数字型数据

    话不多说,根据最近项目需求,为了减少输入验证等相关代码量,需要此控件 先上效果图 默认样式是这样,自己可以根据需求修改外形,但我更喜欢它自带的简洁版 有人可能会问怎么实现的呢?其实很简单,我们设置它的 ...

  5. 入行数字IC验证后会做些什么?

    半年前,公众号写了第一篇推文<入行数字IC验证的一些建议>,介绍了IC小白可以如何一步一步地摸索入门数字IC验证,同时也在知乎发了这篇入门贴,并且衍生出很多额外基础的内容,收获了不少的浏览 ...

  6. P1494 小Z的袜子 莫队

    题干 就是将$add$和$del$函数里的$ans$变化变成组合数嘛, 先预处理出$x$只相同袜子一共有$f[x] = 1+2+...+$$(x-1)$种组合, 要注意,由于$f[x]$是一直加到$x ...

  7. springboot中配置skywalking请求日志

    pom.xml配置 <dependency> <groupId>org.apache.skywalking</groupId> <artifactId> ...

  8. NAT模式 LVS负载均衡群集部署

    NAT模式 LVS负载均衡群集部署的操作步骤 实验环境准备: 负载调度器:内网关 ens33:172.16.10.1,外网关 ens37:12.0.0.1 Web节点服务器1:172.16.10.10 ...

  9. RK3568开发笔记(四):在虚拟机上使用SDK编译制作uboot、kernel和buildroot镜像

    前言   上一篇搭建好了ubuntu宿主机开发环境,本篇的目标系统主要是开发linux+qt,所以需要刷上billdroot+Qt创建的系统,为了更好的熟悉原理和整个开发过程,选择从零开始搭建rk35 ...

  10. Hadoop学习 Hadoop-HA 解释和概念介绍

    一.Hadoop-HA 1.1 Hadoop1.x带来的问题 1.单点故障 a. 每个群集只有一个NameNode,NameNode存在单点故障(SPOF). ​ b. 如果该计算机或进程不可用,则整 ...