• [思维]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. }

  • 相关阅读:
    动态加载内容爬取,Ajax爬取典例
    上传本地包到私有maven仓库
    工程项目常见风险及其22种最佳管理实践
    盘点 | 云原生峰会重磅发布
    Redis入门
    易语言实现植物大战僵尸新手cheat体验
    数据结构——二叉树
    面试官:transient关键字修饰的变量当真不可序列化?我:烦请先生教我!
    Vue 2.0——初识组件
    异常与错误处理高级用法
  • 原文地址:https://blog.csdn.net/m0_55982600/article/details/126373875