快速幂取模 POJ 3761 bubble sort
1 /*
2 题意:求冒泡排序扫描k次能排好序的全排列个数
3 数学:这里有一个反序列表的概念,bj表示在j左边,但大于j的个数。不多说了,我也是看网上的解题报告。
4 详细解释:http://blog.csdn.net/cscj2010/article/details/7820906
5 */
6 #include <cstdio>
7 #include <algorithm>
8 #include <cstring>
9 #include <cmath>
10 using namespace std;
11
12 typedef long long ll;
13 const int MAXN = 1e6 + 10;
14 const int INF = 0x3f3f3f3f;
15 const int MOD = 20100713;
16 ll fact[MAXN];
17
18 void solve(void) {
19 fact[0] = fact[1] = 1;
20 for (int i=2; i<=1000000; ++i) {
21 fact[i] = fact[i-1] * i % MOD;
22 }
23 }
24
25 ll pow_mod(ll x, ll n) {
26 ll ret = 1;
27 while (n) {
28 if (n & 1) ret = ret * x % MOD;
29 x = x * x % MOD;
30 n >>= 1;
31 }
32 return ret;
33 }
34
35 int main(void) { //POJ 3761 bubble sort
36 solve ();
37 int T; scanf ("%d", &T);
38 while (T--) {
39 ll n, k; scanf ("%I64d%I64d", &n, &k);
40 printf ("%I64d\n", (pow_mod (k + 1, n - k) - pow_mod (k, n - k) + MOD) % MOD * fact[k] % MOD);
41 }
42
43 return 0;
44 }
编译人生,运行世界!