Niu Ke Practice Match 64 D Gem Packing (Rejection Theorem, 01 Backpack)

Gem Packing
This question should be easy to think of, if there is no limit, then one-to-one, the answers are all arranged, and then subtract the iiIf the i box is misplaced, then I have to add two misplaced boxes,… \dots , then how to maintain this answer later, let's setdp [i] [j] dp[i][j]d p [ i ] [ j ] is the currentiii boxes, which have been misplaced at this timejjNumber of j solutions, thenf [i] [j] = f [i − 1] [j] + f [i − 1] [j − 1] ∗ a [i] f[i][j]=f [i-1][j]+f[i-1][j-1]*a[i]f[i][j]=f[i1][j]+f[i1][j1]a [ i ]
, considering the one-dimensional optimization, the two-dimensional easily exploded space, getdp [1], dp [2],… dp[1],dp[2], \dotsdp[1],dp[2],…And then multiply each of the following plans by the full array of other gems, and finally get the answer.

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define rush() int T;scanf("%d",&T);while(T--)
#define mm(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define pf(a) printf("%d\n",a)
#define pf2(a,b) printf("%d %d\n",a,b)
#define p_f(a) printf("%d ",a)
#define pyn(a) if(a)puts("Yes");else puts("No");
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const db eps=1e-9;
const int N=8e3+5;
const int P=998244353;
int n,x,a[N];
ll dp[N],fac[N];
int32_t main()
{
    
    
    cin>>n;
    fac[0]=fac[1]=1ll;
    for(int i=2;i<=n;i++)fac[i]=(i*fac[i-1])%P;
    ll ans=fac[n];
    for(int i=1;i<=n;a[x]++,i++)cin>>x;
    dp[0]=1;
    for(int i=1;i<=n;i++)for(int j=i;j>=1;j--)dp[j]=(dp[j]+a[i]*dp[j-1])%P;
    for(int i=1;i<=n;i++)ans=i&1?(ans-dp[i]*fac[n-i]%P+P)%P:(ans+dp[i]*fac[n-i]%P)%P;
    return cout<<ans,0;
}

Guess you like

Origin blog.csdn.net/zhouzi2018/article/details/106300443