题目链接:https://vjudge.net/problem/POJ-3761

转自:https://blog.csdn.net/cscj2010/article/details/7820906

题目大意

   含 n 个不同元素的排列恰好经过 k 趟冒泡排序变得有序。问原数组有多少种排列情况?

分析

  第一眼看上去觉得是个 DP,最后发现是个数学题,认栽。。。
  

  首先,定义 f(x) 表示在数组中位于元素 x 左面且大于 x 的个数。那么有$0 \leq f(x) \leq n - x$。

  定义 g(x) 为经过不超过 x 趟排序的排列数。于是答案就为 g(k) - g(k - 1)。

  由题意得$k = f(x)_{max}$,因为冒泡每次直冒一个数,因此每轮只有一个数会冒到 x 后面,对其他数也是一样,也就是每一趟排序$对\forall_{1 \leq x \leq n, f(x) > 0} f(x) = f(x) - 1$。

  接下来推 g(k)。

  由于$k \geq n - x$,所以$x \geq n - k$。

  所以当$x \geq n - k$时,放哪里都可以;而当$x < n - k$时,某些地方是不能放的。

  先放后 k 个数,一种有 k! 种排列。

  对于前 n - k 个数,从小到大排列,然后与后 k 个数的排列合并:$[1, 2, 3, \dots, n - k, a_1, a_2, \dots, a_k]$。

  从 1 开始,1 可以不动,也可以与数组中比它大的前 k 个数中的某一个对调,有 k + 1 种。

  2 在 1 的基础上同理,也有 k + 1 种。

  于是前 n - k 个数总共有$(k + 1)^{n - k}$种。

  那这样做会不会有重复呢?不可能,因为是从小到大遍历的,所以 x 一旦调出去,就不可能再被调回来。

  因此$g(k) = (k + 1)^{n - k} * k!$。

  所以$g(k) - g(k - 1) = k! * ((k + 1)^{n - k} - k^{n - k})$。

代码如下

 #include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <stack>
#include <deque>
#include <list>
#include <sstream>
#include <cassert>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< LL, int > MLLI;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x3fffffff;
const LL infLL = 0x3fffffffffffffffLL;
const LL mod = ;
const int maxN = 1e6 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; LL T, N, K, ans; LL mul_mod(LL a, LL b) {
return (a * b) % mod;
} LL sub_mod(LL a, LL b) {
return (a - b + mod) % mod;
} LL fac[maxN];
void init_fact() {
fac[] = ;
For(i, , maxN - ) fac[i] = (i * fac[i - ]) % mod;
} inline LL pow_mod(LL x, LL y, LL p = mod){
LL ret = ;
while(y){
if(y & ) ret = (ret * x) % p;
x = (x * x) % p;
y >>= ;
}
return ret;
} int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
//INIT();
init_fact();
scanf("%lld", &T);
while(T--) {
scanf("%lld%lld", &N, &K);
ans = mul_mod(fac[K], sub_mod(pow_mod(K + , N - K), pow_mod(K, N - K)));
printf("%lld\n", ans);
}
return ;
}

最新文章

  1. Azure China (5) 管理Azure China Powershell
  2. Javascript 模块化开发上线解决方案
  3. Jquery实现滚动到底部加载更多(最原始)
  4. C语言之强制类型转换与指针--#define DIR *((volatile unsigned int *) 0x0022)
  5. C++ API设计
  6. 【二进制拆分多重背包】【HDU1059】【Dividing】
  7. Cannot declare class app\home\controller\Cases because the name is already in use
  8. 安装windows 10到固态硬盘实践记录
  9. Android多种格式的异步解压/压缩解决方案
  10. App调试的几个命令实践【转】
  11. RN与android原生开发混合后的环境报错问题
  12. window.location.herf=url参数有中文,到后台乱码问题解决
  13. while 解决 10000米绳子 对折当 绳长小于5米时求绳的对折次数
  14. jquery clone 获取文本框值得问题
  15. NO--15 微信小程序,scroll-view选项卡和跳转
  16. VMware 增加硬盘ubuntu
  17. Java 异常处理之 论 finally块何时候不走
  18. A brief introduction to Hashing and Rehashing
  19. python中的循环和编码,运算符, 格式化输出
  20. [C语言] 数据结构概述

热门文章

  1. JSTL标签的用法详解
  2. [bzoj2729][HNOI2012]排队 题解 (排列组合 高精)
  3. (转)OpenFire源码学习之八:MUC用户聊天室
  4. windows10安装sqlmap与简单配置
  5. mongo分组查询(转)
  6. Dubbo入门到精通学习笔记(六):持续集成管理平台之Hudson 持续集成服务器的安装配置与使用
  7. 剑指offer——60二叉树的深度
  8. 用注解实现SpringMvc
  9. Pandas之read_excel()和to_excel()函数解析
  10. .net core 根据环境变量区分正式、测试 配置文件