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:
- Take two balls from the box, uniformly and independently at random.
- 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;
}