bzoj 4517: [Sdoi2016]排列计数
生活随笔
收集整理的這篇文章主要介紹了
bzoj 4517: [Sdoi2016]排列计数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
4517: [Sdoi2016]排列計數
Time Limit:?60 Sec??Memory Limit:?128 MBSubmit:?637??Solved:?396
[Submit][Status][Discuss]
Description
求有多少種長度為 n 的序列 A,滿足以下條件: 1 ~ n 這 n 個數在序列中各出現了一次 若第 i 個數 A[i] 的值為 i,則稱 i 是穩(wěn)定的。序列恰好有 m 個數是穩(wěn)定的 滿足條件的序列可能很多,序列數對 10^9+7 取模。Input
第一行一個數 T,表示有 T 組數據。 接下來 T 行,每行兩個整數 n、m。 T=500000,n≤1000000,m≤1000000Output
輸出 T 行,每行一個數,表示求出的序列數
Sample Input
51 0
1 1
5 2
100 50
10000 5000
Sample Output
01
20
578028887
60695423
HINT
Source
鳴謝Menci上傳
?
?
答案為C(n,m)*f[n-m],f[n-m]表示(n-m)個數錯排的方案數。
錯排問題戳這里
Fn=(n-1)(Fn-1+Fn-2)
預處理即可。
?
1 #include<bits/stdc++.h> 2 #define LL long long 3 const int oo = 0x3f3f3f3f; 4 template <class T> inline bool Chkmin(T &x, T y) { return x > y ? x = y, true : false; } 5 template <class T> inline bool Chkmax(T &x, T y) { return x < y ? x = y, true : false; } 6 template <class T> inline T read(T &x) 7 { 8 static int f; 9 static char c; 10 for (f = 1; !isdigit(c = getchar()); ) if (c == '-') f = -f; 11 for (x = 0; isdigit(c); c = getchar()) x = x * 10 + c - 48; 12 return x *= f; 13 } 14 template <class T> inline void write(T x, char P = 0) 15 { 16 static char s[20]; 17 static int top = 0; 18 if (x < 0) x = -x, putchar('-'); 19 do {s[++top] = x % 10 + 48;} while (x /= 10); 20 do {putchar(s[top]);} while (--top); 21 if (P) putchar(P); 22 } 23 24 static const int MAXN = 1e6; 25 static const int MO = 1e9 + 7; 26 27 static int T, N, M; 28 static int Jc[MAXN + 5], Ni[MAXN + 5], f[MAXN + 5]; 29 30 inline int Qpow(int x, int n) 31 { 32 int ans = 1; 33 while (n) { 34 if (n & 1) ans = (LL) ans * x % MO; 35 x = (LL) x * x % MO; 36 n >>= 1; 37 } 38 return ans; 39 } 40 41 int main() 42 { 43 Ni[0] = Jc[0] = 1; 44 for (register int i = 1; i <= MAXN; ++ i) { 45 Jc[i] = (LL) Jc[i - 1] * i % MO; 46 Ni[i] = Qpow(Jc[i], MO - 2); 47 } 48 f[0] = 1; f[1] = 0; 49 for (register int i = 2; i <= MAXN; ++ i) { 50 f[i] = (LL) (f[i - 1] + f[i - 2]) * (i - 1) % MO; 51 } 52 for (read(T); T--; ) { 53 read(N), read(M); 54 write((LL) Jc[N] * Ni[N - M] % MO * Ni[M] % MO * f[N - M] % MO, '\n'); 55 } 56 return 0; 57 } View Code?
轉載于:https://www.cnblogs.com/jszyxw/p/5508409.html
總結
以上是生活随笔為你收集整理的bzoj 4517: [Sdoi2016]排列计数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sys
- 下一篇: 自制反汇编逆向分析工具 迭代第六版本 (