[思维]Sum Plus Product 2022杭电多校第9场 1010

59 篇文章 0 订阅

Problem Description

triplea has a box with n(n≥1)n(n≥1) balls inside, where on each of the balls there is an integer written. When there are at least two balls inside the box, triplea will do the following operation repeatedly:

  1. Take two balls from the box, uniformly and independently at random. 
  2. Supposes the numbers written on the two balls are aa and bb, respectively, then tripleawill put a new ball in the box on which a number S+PS+P is written, where S=a+bS=a+b is the sum of aa and bb, and P=abP=ab is the product of aa and bb. 

The operation will end when there is only one ball in the box. triplea wonders, what is the expected value of the number written on the last ball? He gets the answer immediately, and leaves this as an exercise for the reader, namely, you.

Input

The first line of input consists of an integer T(1≤T≤20)T(1≤T≤20), denoting the number of test cases.

For each test case, the first line of input consists of an integer n(1≤n≤500)n(1≤n≤500), denoting the initial number of balls inside the box.

The next line contains nn integers a1,a2,…,an(0≤ai<998244353)a1​,a2​,…,an​(0≤ai​<998244353), denoting the number written on each ball in the box, initially.

Output

For each test case, output an integer in one line, denoting the expected value of the number written on the last ball. Under the input constraints of this problem, it can be shown that the answer can be written as PQQP​, where PP and QQ are coprime integers and Q≢0(mod998244353)Q≡0(mod998244353). You need to output P⋅Q−1(mod998244353)P⋅Q−1(mod998244353) as an answer, where Q−1Q−1 is the modular inverse of QQ with respect to 998244353998244353.

Sample Input

2

2

2 2

10

1 2 4 8 16 32 64 128 256 512

Sample Output

8

579063023

​​​​​​​题意: 有n个带数字的小球,可以随机选择其中两个小球进行合并,合并后得到一个新的小球,其上的数字为a*b+a+b,问合并到最后只剩一个小球时其上数字的期望。

分析: a*b+a+b等于(a+1)*(b+1)-1,所以无论合并到哪一步所有数字的a+1乘积再-1最终都不变,所以合并到最后一个小球上的值就是所有数字+1的乘积再-1。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#define int long long
using namespace std;

const int mod = 998244353;

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int n;
		cin >> n;
		int ans = 1;
		for(int i = 1; i <= n; i++){
			int t;
			scanf("%lld", &t);
			ans = (ans*(t+1))%mod;
		}	
		cout << ((ans-1)%mod+mod)%mod << endl;
	}
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值