POJ 3761 Bubble Sort

题目链接: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})$。

代码如下

  1 #include <cmath>
  2 #include <ctime>
  3 #include <iostream>
  4 #include <string>
  5 #include <vector>
  6 #include <cstdio>
  7 #include <cstdlib>
  8 #include <cstring>
  9 #include <queue>
 10 #include <map>
 11 #include <set>
 12 #include <algorithm>
 13 #include <cctype>
 14 #include <stack>
 15 #include <deque>
 16 #include <list>
 17 #include <sstream>
 18 #include <cassert>
 19 using namespace std;
 20  
 21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 22 #define Rep(i,n) for (int i = 0; i < (n); ++i)
 23 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
 24 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
 25 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 26 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 27 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 28 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 29  
 30 #define pr(x) cout << #x << " = " << x << "  "
 31 #define prln(x) cout << #x << " = " << x << endl
 32  
 33 #define LOWBIT(x) ((x)&(-x))
 34  
 35 #define ALL(x) x.begin(),x.end()
 36 #define INS(x) inserter(x,x.begin())
 37 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
 38 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 
 39 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
 40 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
 41  
 42 #define ms0(a) memset(a,0,sizeof(a))
 43 #define msI(a) memset(a,inf,sizeof(a))
 44 #define msM(a) memset(a,-1,sizeof(a))
 45 
 46 #define MP make_pair
 47 #define PB push_back
 48 #define ft first
 49 #define sd second
 50  
 51 template<typename T1, typename T2>
 52 istream &operator>>(istream &in, pair<T1, T2> &p) {
 53     in >> p.first >> p.second;
 54     return in;
 55 }
 56  
 57 template<typename T>
 58 istream &operator>>(istream &in, vector<T> &v) {
 59     for (auto &x: v)
 60         in >> x;
 61     return in;
 62 }
 63  
 64 template<typename T1, typename T2>
 65 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 66     out << "[" << p.first << ", " << p.second << "]" << "
";
 67     return out;
 68 }
 69 
 70 inline int gc(){
 71     static const int BUF = 1e7;
 72     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 73     
 74     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 75     return *bg++;
 76 } 
 77 
 78 inline int ri(){
 79     int x = 0, f = 1, c = gc();
 80     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 81     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 82     return x*f;
 83 }
 84 
 85 template<class T>
 86 inline string toString(T x) {
 87     ostringstream sout;
 88     sout << x;
 89     return sout.str();
 90 }
 91 
 92 inline int toInt(string s) {
 93     int v;
 94     istringstream sin(s);
 95     sin >> v;
 96     return v;
 97 }
 98 
 99 //min <= aim <= max
100 template<typename T>
101 inline bool BETWEEN(const T aim, const T min, const T max) {
102     return min <= aim && aim <= max;
103 }
104  
105 typedef long long LL;
106 typedef unsigned long long uLL;
107 typedef pair< double, double > PDD;
108 typedef pair< int, int > PII;
109 typedef pair< int, PII > PIPII;
110 typedef pair< string, int > PSI;
111 typedef pair< int, PSI > PIPSI;
112 typedef set< int > SI;
113 typedef set< PII > SPII;
114 typedef vector< int > VI;
115 typedef vector< double > VD;
116 typedef vector< VI > VVI;
117 typedef vector< SI > VSI;
118 typedef vector< PII > VPII;
119 typedef map< int, int > MII;
120 typedef map< LL, int > MLLI;
121 typedef map< int, string > MIS;
122 typedef map< int, PII > MIPII;
123 typedef map< PII, int > MPIII;
124 typedef map< string, int > MSI;
125 typedef map< string, string > MSS;
126 typedef map< PII, string > MPIIS;
127 typedef map< PII, PII > MPIIPII;
128 typedef multimap< int, int > MMII;
129 typedef multimap< string, int > MMSI;
130 //typedef unordered_map< int, int > uMII;
131 typedef pair< LL, LL > PLL;
132 typedef vector< LL > VL;
133 typedef vector< VL > VVL;
134 typedef priority_queue< int > PQIMax;
135 typedef priority_queue< int, VI, greater< int > > PQIMin;
136 const double EPS = 1e-8;
137 const LL inf = 0x3fffffff;
138 const LL infLL = 0x3fffffffffffffffLL;
139 const LL mod = 20100713;
140 const int maxN = 1e6 + 7;
141 const LL ONE = 1;
142 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
143 const LL oddBits = 0x5555555555555555;
144 
145 LL T, N, K, ans;
146 
147 LL mul_mod(LL a, LL b) {
148     return (a * b) % mod;
149 }
150 
151 LL sub_mod(LL a, LL b) {
152     return (a - b + mod) % mod;
153 }
154 
155 LL fac[maxN];
156 void init_fact() {
157     fac[0] = 1;
158     For(i, 1, maxN - 1) fac[i] = (i * fac[i - 1]) % mod;
159 }
160 
161 inline LL pow_mod(LL x, LL y, LL p = mod){
162     LL ret = 1;
163     while(y){
164         if(y & 1) ret = (ret * x) % p;
165         x = (x * x) % p;
166         y >>= 1;
167     }
168     return ret;
169 } 
170 
171 int main(){
172     //freopen("MyOutput.txt","w",stdout);
173     //freopen("input.txt","r",stdin);
174     //INIT();
175     init_fact();
176     scanf("%lld", &T);
177     while(T--) {
178         scanf("%lld%lld", &N, &K);
179         ans = mul_mod(fac[K], sub_mod(pow_mod(K + 1, N - K), pow_mod(K, N - K)));
180         printf("%lld
", ans);
181     }
182     return 0;
183 }
View Code
原文地址:https://www.cnblogs.com/zaq19970105/p/11249151.html