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


    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。

    具体代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #define int long long
    8. using namespace std;
    9. const int mod = 998244353;
    10. signed main()
    11. {
    12. int T;
    13. cin >> T;
    14. while(T--){
    15. int n;
    16. cin >> n;
    17. int ans = 1;
    18. for(int i = 1; i <= n; i++){
    19. int t;
    20. scanf("%lld", &t);
    21. ans = (ans*(t+1))%mod;
    22. }
    23. cout << ((ans-1)%mod+mod)%mod << endl;
    24. }
    25. return 0;
    26. }

  • 相关阅读:
    浮点数在内存中的存储
    React版/Vue版都齐了,开源一套【特别】的后台管理系统...
    2.1 Elasticsearch DSL搜索-数据准备
    JDBC完成对数据库数据操作(增,删,改,查)
    Nginx安装与虚拟主机配置shell脚本
    Golang 递归获取目录下所有文件
    ES6中的set、map
    【SpingBoot拦截器】实现两个接口,配置拦截路径
    基于Java毕业设计学生在线评教系统源码+系统+mysql+lw文档+部署软件
    多维时序 | MATLAB实现ELM极限学习机多维时序预测(股票价格预测)
  • 原文地址:https://blog.csdn.net/m0_55982600/article/details/126373875