BZOJ4517 [Sdoi2016]排列计数 【组合数 + dp】

题目

1 ~ n 这 n 个数在序列中各出现了一次

输入格式

T=500000，n≤1000000，m≤1000000

5

1 0

1 1

5 2

100 50

10000 5000

0

1

20

578028887

60695423

题解

①如果放在自由位置，那么剩余的数成了$f[i - 1]$
②如果不放在自由位置，有$i - 1$个位置，剩余$i - 1$的方案就是$g[i - 1]$

$f[i] = (i - 1) * g[i - 1]$

$g[i] = f[i - 1] + (i - 1) * g[i - 1]$

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000,P = 1000000007;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
LL fac[maxn],inv[maxn],fv[maxn],f[maxn],g[maxn];
int n,m,T;
LL C(int x,int y){
return fac[x] * fv[y] % P * fv[x - y] % P;
}
int main(){
fac[0] = 1;
for (int i = 1; i <= 1000000; i++) fac[i] = fac[i - 1] * i % P;
inv[0] = inv[1] = 1;
for (int i = 2; i <= 1000000; i++) inv[i] = (P - P / i) * inv[P % i] % P;
fv[0] = 1;
for (int i = 1; i <= 1000000; i++) fv[i] = fv[i - 1] * inv[i] % P;
f[0] = 1; f[1] = 0; g[1] = 1; g[0] = 1;
for (int i = 2; i <= 1000000; i++){
f[i] = (i - 1) * g[i - 1] % P;
g[i] = (f[i - 1] + (i - 1) * g[i - 1] % P) % P;
}