牛客练习赛64 D-宝石装箱(容斥定律,背包)

链接:https://ac.nowcoder.com/acm/contest/5633/D
来源:牛客网

题意:

n颗宝石装进n个箱子使得每个箱子中都有一颗宝石。第i颗宝石不能装入第ai个箱子。求合法的装箱方案对998244353取模。

两种装箱方案不同当且仅当两种方案中存在一颗编号相同的宝石装在 不同编号的箱子中。

思路:

\(cnt_i\)代表第\(\mathit i\)个箱子不能装入的宝石数量。

\(g_x\)为有\(\mathit x\)个箱子装了不合法的宝石的方案数,

\(g_x\)可以通过背包形式的动态规划来求解:

初始:\(g_0=1,g_k=0(k>1)\)

转移:对于每一个箱子\(\mathit i\),我们更新\(g_{k}=g_k+g_{k-1}*a_i,k\in[1,i]\)

\(f_x\)为有\(\mathit x\)个箱子可以任意放\(\mathit x\)个宝石的方案数,则\(f_x=x!\)

那么答案即为:\(ans=\sum_{i=0}^{n}(-1)^i*g_i*f_{n-i}\)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <unordered_map>
// #include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
ll g[maxn];
ll f[maxn];
ll a[maxn];
const ll mod = 998244353;
int main()
{
#if DEBUG_Switch
    freopen("C:\\code\\input.txt", "r", stdin);
#endif
    //freopen("C:\\code\\output.txt","r",stdin);
    n = readint();
    int x;
    f[0] = 1ll;
    repd(i, 1, n)
    {
        f[i] = f[i - 1] * i % mod;
        x = readint();
        a[x]++;
    }
    g[0] = 1ll;
    repd(i, 1, n)
    {
        for (int j = i - 1; j >= 0; --j)
        {
            g[j + 1] += g[j] * a[i] % mod;
            g[j + 1] %= mod;
        }
    }
    ll ans = 0ll;
    for (int i = 0, p = 1; i <= n; ++i, p *= -1)
    {
        ans += g[i] * f[n - i] * p % mod;
        ans = (ans + mod) % mod;
    }
    printf("%lld\n", ans );
    return 0;
}