题面十分简单,n个球放m个盒子,可以空,盒子不同,要求球数最多的盒子唯一,方案数
n,m都是500,考虑复杂度O(n3)
首先O(n)的枚举最多的球数,假设第一个盒子最多
然后考虑后面m−1个盒子,放n−x个球,每个盒子小于x个
所以可以考虑dp状态,dp[i][j]表示前i个盒子,放了j格球且每个盒子小于x的方案
dp[i][j]=dp[i−1][j]+dp[i−1][j−1]+⋯+dp[i−1][j−x+1]
这是O(nm)的状态,O(n)的转移,可能会T
但是仔细观察这个转移,其实可以前缀和优化
dp[i][j]=dp[i][j−1]−dp[i−1][j−x]+dp[i−1][j],就能实现O(1)的转移
总复杂度O(n3)
不贴代码了
蓝儿这题还有一种O(n2)的容斥方法
先O(n)的枚举第一个盒子最多x个
然后ans=∑F(n−x,m−1,x)
F(n,m,x)=∑mi=0(−1)iC(m,i)f(n−ix,m)
f表示n个球放进m个盒子的所有情况
为什么这么容斥呢,因为有x1+x2+⋯+xm=n,并且xi≤s
所以容斥就是考虑没有限制条件,有一个限制条件,有两个。。。
这样搞一下就行了
其实这也是我会的东西,但是比赛时候没有考虑到啊GG好弱
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 200005
#define MAXN 1000005
#define maxnode 133
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-8;
const LL mod = 998244353;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
LL fact[1005];
LL qpow(LL a,LL n){
LL ans=1;
while(n){
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
LL C(int n,int m){
return (fact[n]*qpow(fact[n-m],mod-2)%mod)*qpow(fact[m],mod-2)%mod;
}
LL f(int n,int m){
return C(n+m-1,m-1);
}
LL F(int n,int m,int x){
LL ans=0;
for(int i=0;i<=m;i++){
if(n-i*x<0) break;
if(i%2) ans-=C(m,i)*f(n-i*x,m)%mod;
else ans+=C(m,i)*f(n-i*x,m)%mod;
}
return (ans%mod+mod)%mod;
}
int main(){
int n,m;
cin>>n>>m;
if(m==1){
cout<<1<<endl;
return 0;
}
LL ans=0;
fact[0]=1;
for(int i=1;i<=1000;i++) fact[i]=fact[i-1]*i%mod;
for(int i=n/m;i<=n;i++){
ans=(ans+F(n-i,m-1,i))%mod;
}
cout<<ans*m%mod<<endl;
return 0;
}