数学考试
思路:
fx表示1-x的排列对于前(pi<x)的限制都满足,但是不满足x的限制条件的个数。fx初始为n!现在要去掉不合法的,为了避免重复计算,应该减去所有pi的贡献,即fpi(n-pi)!。
*代码:**
#include<bits/stdc++.h> #define int long long using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); const int mod = 20000311; int n, m; cin >> n >> m; vector<int> a(m + 10); for(int i = 1; i <= m; i++) { cin >> a[i]; } sort(a.begin()+1, a.begin() + m + 1); a[m + 1] = n; vector<int> fac(n+10); fac[0] = 1; for(int i = 1; i <= n; i++) { fac[i] = (fac[i-1] * i) % mod; } vector<int> dp(n+10); for(int i = 1; i <= m + 1; i++) { dp[i] = fac[a[i]]; for(int j = 1; j < i; j++) { dp[i] = (dp[i] - dp[j] * fac[a[i] - a[j]] % mod + mod) % mod; } } cout << dp[m + 1] << endl; return 0; }