跳到正文

题解

题解:AT_arc217_c [ARC217C] Greedy Customers 2

recloud约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年4月10日
收录
2026年4月10日
更新
2026年11月15日
篇幅
约 7 分钟
获赞
1

版本

  1. v2当前
  2. v1

题意:

给你 nn 个货物,价值为 a1∼na_{1 \sim n},然后有 nn 个人来买东西,每个人的钱数随机在 [1,c][1,c] 里面,随机到的钱数如果比最贵的物品贵,就买下,问恰好购买 kk 个物品的概率,kk 是 [0,n][0,n]。

对 998244353998244353 取模,n≤100n \le 100,1≤ai≤c<9982443531\le a_i\le c< 998244353。

做法:

dp。

对 aa 排序。

fi,j,kf_{i,j,k} 表示前 ii 个价格,有 jj 个人的预算大于等于 aia_i,此时至少买了 kk 件的方案数。

转移的时候枚举 ii,jj,kk 后再枚举一维 xx 表示接下来 xx 个人确定了预算大于了 aia_i 但是小于 ai+1a_{i+1}。

所有现在合法的人数 J=j+xJ=j+x 中选出 xx 个人在 [ai,ai+1][a_{i},a_{i+1}] 中的系数为 (Jx)\binom{J}{x}。

xx 个人的钱数在 [ai,ai+1][a_{i},a_{i+1}],系数为 (ai+1−ai)x(a_{i+1}-a_i)^x。

fi−1,j,k→fi,j+x,x+kf_{i-1,j,k}\to f_{i,j+x,x+k}。

时间复杂度 O(n4)O(n^4)。

代码:

C++
/*
雲璃猫猫が好きです
すべての生命よ,歌のように輝いています
截剣式、斬、断、破です!
*/
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
#define int long long
#define INF 1e18
#define lb long double
#define ls (id<<1)
#define rs (id<<1|1)
#define rep(i,l,r,k) for(int i=(l);i<=(r);i+=(k))
#define dep(i,r,l,k) for(int i=(r);i>=(l);i-=(k))
#define tep(ch,cr) for(auto ch:cr)
#define mk make_pair
#define me(a,b) memset(a,b,sizeof(a))
#define pb emplace_back
#define fi first
#define se second
#define wl while
#define max(a,b)((a)>(b)?(a):(b))
#define min(a,b)((a)<(b)?(a):(b))
#define ce continue
#define bk break
#define sr stable_sort
#define re return
#define vd void()
#define ve vector
#define fb fabs
#define ab abs
random_device rd;
unsigned int seed=rd();
mt19937 Rand(seed);
typedef pair<int,int> pii;
__gnu_pbds::gp_hash_table<string,int>ml;
namespace recloud{
#define pr putchar
#define debug puts("____________________________________________________________________________")
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
	inline int read(){int sum=0,k=1;char c=gc();
		while(c>'9'||c<'0'){if(c=='-')k=-1;c=gc();
		}while(c>='0'&&c<='9'){sum=sum*10+c-48;c=gc();
		}return sum*k;
	}inline void wr(int x){if(x<0) putchar('-'),x=-x;
		if(x>9) wr(x/10);return void(putchar(x%10+'0'));}}
using namespace recloud;
namespace iosfast{
	inline void init(){
		ios::sync_with_stdio(false); 
		cin.tie(nullptr); 
	}
	inline int read(){
		int x;cin>>x;re x;
	}
	inline void wr(int x){
		cout<<x;
	}
	inline void pr(int x){
		cout<<(char)x;
	}}
//using namespace iosfast;
const int M=2e5+110,mod=1e9+7,Mod=998244353;
namespace MD{
	int md=Mod;
	inline int qp(int x,int y,int res=1){
		wl(y){if(y&1)res=res*x%md;x=x*x%md;y>>=1;}
		return res;
	}
	inline int add(int a,int b){
		return (a+b+md)%md;
	}
	inline int mul(int a,int b){
		return ((a%md)*(b%md))%md;
	}
	int inv[M],jc[M],ijc[M];
	inline void init(int n){
		inv[0]=jc[0]=ijc[0]=inv[1]=jc[1]=ijc[1]=1;
		rep(i,2,n,1)
		inv[i]=(md-md/i)*inv[md%i]%md,
		jc[i]=jc[i-1]*i%md,
		ijc[i]=ijc[i-1]*inv[i]%md;
	}
	inline int C(int x,int y){
		if(x<0||y<0||x<y) return 0;
		return jc[x]*ijc[x-y]%md*ijc[y]%md;
	}
};using namespace MD;
int n,c,a[M],f[105][105][105],ooo;
inline void work(){
	n=read(),c=read();ooo=qp(qp(c,n),md-2);
	rep(i,1,n,1) a[i]=read();
	sr(a+1,a+1+n);a[n+1]=c+1;me(f,0);
	rep(i,1,n,1) 
		f[0][i][0]=qp(a[1]-1,i);
	f[0][0][0]=1;
	rep(i,1,n,1) rep(j,0,n,1) rep(k,0,min(i,j),1) rep(x,0,n-j,1){
		int K=min(i,x+k),J=j+x;
		//由于处理到当前只考虑前i件物品,所以 K 要取min(i,x+k)
		f[i][J][K]=add(f[i][J][K],mul(f[i-1][j][k],mul(C(J,x),qp(a[i+1]-a[i],x))));

	}
	rep(i,0,n,1) wr(mul(f[n][n][i],ooo)),pr(32);pr(10);
}

inline void test(int x){int T=x;wl(T--) work();}
signed main(){
	init(100);
	test(read());
	return 0;
}
/*

*/
原文