【CF1349D】 Slime and Biscuits

题目

题目描述
Slime and his nn friends are at a party. Slime has designed a game for his friends to play.

At the beginning of the game, the ii -th player has a_ia
i

biscuits. At each second, Slime will choose a biscuit randomly uniformly among all a_1 + a_2 + \ldots + a_na
1

+a
2

+…+a
n

biscuits, and the owner of this biscuit will give it to a random uniform player among n-1n−1 players except himself. The game stops when one person will have all the biscuits.

As the host of the party, Slime wants to know the expected value of the time that the game will last, to hold the next activity on time.

For convenience, as the answer can be represented as a rational number \frac{p}{q}
q
p

for coprime pp and qq , you need to find the value of (p \cdot q^{-1})\mod 998,244,353(p⋅q
−1
)mod998244353 . You can prove that q\mod 998,244,353 \neq 0qmod998244353


=0 .

输入格式
The first line contains one integer n\ (2\le n\le 100,000)n (2≤n≤100000) : the number of people playing the game.

The second line contains nn non-negative integers a_1,a_2,\dots,a_n\ (1\le a_1+a_2+\dots+a_n\le 300,000)a
1

,a
2

,…,a
n

(1≤a
1

+a
2

+⋯+a
n

≤300000) , where a_ia
i

represents the number of biscuits the ii -th person own at the beginning.

输出格式
Print one integer: the expected value of the time that the game will last, modulo 998,244,353998244353 .

题意翻译
有 nn 个人, 第 ii 个人有 a_ia
i

个饼干.
每次随机选择一个饼干, 将其随机分配给除了它现在所有者的其他 n-1n−1 个人.
求使得一个人拥有所有饼干的期望步数, 对 998244353998244353 取模.
输入输出样例
输入 #1复制
2
1 1
输出 #1复制
1
输入 #2复制
2
1 2
输出 #2复制
3
输入 #3复制
5
0 0 0 0 35
输出 #3复制
0
输入 #4复制
5
8 4 2 0 1
输出 #4复制
801604029
说明/提示
For the first example, in the first second, the probability that player 11 will give the player 22 a biscuit is \frac{1}{2}
2
1

, and the probability that player 22 will give the player 11 a biscuit is \frac{1}{2}
2
1

. But anyway, the game will stop after exactly 11 second because only one player will occupy all biscuits after 11 second, so the answer is 11 .

思路

这里我直接推荐一篇博文,讲的非常好
https://www.luogu.com.cn/blog/Caro23333/codeforces-round-641-div1d-slime-and-biscuits-zhong-wen-ti-jie

代码

#include<bits/stdc++.h>

using namespace std;
const int yjy=998244353;
int n,m;
ll a[100005],ans[300005];

ll power(ll x,ll t)
{
	ll b=1;
	while(t)
	{
		if(t&1) b=b*x%mod;
		x=x*x%mod; t>>=1;
	}
	return b;
}

int main(){
	scanf("%d",&n);
	for(int i=1; i<=n; i++) scanf("%d",&a[i]),m+=a[i];
	ll inv=power(m,yjy-2),invn1=power(n-1,yjy-2);
	for(int i=m; i>=1; i--)
	{
		ll k1=i*inv%yjy*invn1%yjy,k2=(m-i)*inv%yjy;
		ans[i]=(k2*ans[i+1]+1)%yjy*power(k1,yjy-2)%yjy;
	}
	for(int i=1; i<=m; i++) ans[i]=(ans[i]+ans[i-1])%yjy;
	ll aii=0;
	for(int i=1; i<=n; i++) aii=(aii+ans[m-a[i]])%yjy;
	aii=(aii+yjy-ans[m]*(n-1)%yjy)%yjy;
	printf("%lld\n",aii*power(n,yjy-2)%yjy);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值