首页 > 其他分享 >D. Colored Balls

D. Colored Balls

时间:2024-04-13 16:12:49浏览次数:21  
标签:Balls Colored sum balls value colors set its

D. Colored Balls

There are balls of $n$ different colors; the number of balls of the $i$-th color is $a_i$.

The balls can be combined into groups. Each group should contain at most $2$ balls, and no more than $1$ ball of each color.

Consider all $2^n$ sets of colors. For a set of colors, let's denote its value as the minimum number of groups the balls of those colors can be distributed into. For example, if there are three colors with $3$, $1$ and $7$ balls respectively, they can be combined into $7$ groups (and not less than $7$), so the value of that set of colors is $7$.

Your task is to calculate the sum of values over all $2^n$ possible sets of colors. Since the answer may be too large, print it modulo $998\,244\,353$.

Input

The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of colors.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 5000$) — the number of balls of the $i$-th color.

Additional constraint on input: the total number of balls doesn't exceed $5000$.

Output

Print a single integer — the sum of values of all $2^n$ sets of colors, taken modulo $998\,244\,353$.

Examples

input

3
1 1 2

output

11

input

1
5

output

5

input

4
1 3 3 7

output

76

Note

Consider the first example. There are $8$ sets of colors:

  • for the empty set, its value is $0$;
  • for the set $\{1\}$, its value is $1$;
  • for the set $\{2\}$, its value is $1$;
  • for the set $\{3\}$, its value is $2$;
  • for the set $\{1,2\}$, its value is $1$;
  • for the set $\{1,3\}$, its value is $2$;
  • for the set $\{2,3\}$, its value is $2$;
  • for the set $\{1,2,3\}$, its value is $2$.

So, the sum of values over all $2^n$ sets of colors is $11$.

 

解题思路

  题目给出的组合规则其实就是摩尔投票。即确定颜色的方案后,假设球的总数为 $m$,所有颜色中数量最多的球有 $x$ 个,则分组数量的最小值分两种情况:

  • 如果 $x \leq m-x$,则至少分成 $\left\lceil \frac{n}{2} \right\rceil$ 组。
  • 如果 $x > m-x$,则至少分成 $x$ 组。

  所以我们可以对数组 $a$ 升序排序,固定数量最多的球 $a_i$,用 $f(i-1,j)$ 来表示所有不超过 $a_i$ 的球(即 $a_1 \sim a_{i-1}$)组成总数为 $j$ 的方案数量。那么以 $a_i$ 作为数量最多的球的所有方案中,贡献的答案是(其中 $m = \sum{a_i}$):

$$\sum\limits_{j=0}^{a_i}{\left\lceil \frac{j+a_i}{2} \right\rceil \cdot f(i-1,j)} + \sum\limits_{j=a_i+1}^{m}{a_i \cdot f(i-1,j)}$$

  其中 $f(i, j)$ 根据是否选择 $a_i$ 进行状态划分(01 背包),转移方程为 $f(i,j) = f(i-1,j) + f(i-1,j-a_i)$。

  AC 代码如下,时间复杂度为 $O(n \cdot \sum{a_i})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 5005, mod = 998244353;

int a[N];
int f[N][N];

int main() {
    int n, m = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
        m += a[i];
    }
    sort(a + 1, a + n + 1);
    int ret = 0;
    f[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if (j >= a[i]) ret = (ret + (j + a[i] + 1ll) / 2 * f[i - 1][j]) % mod;
            else ret = (ret + 1ll * a[i] * f[i - 1][j]) % mod;
            f[i][j] = f[i - 1][j];
            if (j >= a[i]) f[i][j] = (f[i][j] + f[i - 1][j - a[i]]) % mod;
        }
    }
    printf("%d", ret);
    
    return 0;
}

 

参考资料

  Educational Codeforces Round 164 (Rated for Div. 2) A~E - 知乎:https://zhuanlan.zhihu.com/p/692227075

标签:Balls,Colored,sum,balls,value,colors,set,its
From: https://www.cnblogs.com/onlyblues/p/18132986

相关文章

  • 14 CodeTON Round 5 (Div. 1 + Div. 2, Rated, Prizes!)C. Tenzing and Balls(dp+前缀
    思路:dp还是挺明显的,思路可以参考最长上升子序列有点dp的感觉\(f[i]\)表示考虑前\(i\)个数,的最大值当前数有两种删或不删不删:\(f[i]=f[i-1]\);删:\(f[i]=max{f[j-1]+i-j+1}\)这个转移是\(O(n^2)\)的显然时间上来不及考虑优化,第一层循环一定是省不了的考虑优化掉第二层循环......
  • [AGC037B] RGB Balls
    题意有\(n\)个人,\(3\timesn\)个球,球有三种颜色,每种颜色恰好\(n\)个。给每个人每种颜色的球各一个,按照在原序列的顺序分别设为\(p1,p2,p3\)。试求使得\(\sump_3-p_1\)最小的方案数。Sol其实直接考虑就行了,没必要想那么复杂。假设当前的球的颜色为\(R\),之前......
  • [Qt-ColorEditor] Qt颜色编辑器,QColorDialog的优化版,支持RGB和HSV等多种方式选色
    外观分享一下我实现的颜色编辑器,主要原因是Qt的QColorDialog功能较少没法满足需求,目前已经在zeno中使用了,由于zeno有自己的样式表,所以在zeno里长这样:如果不加样式表的话长这样:功能srgb切换颜色轮选色颜色文字选色颜色滑动条选色,RGB和HSV上一个/当前颜色切换,这个主要......
  • 小球游戏 -- balls in a line
    ;Ballsinaline;withA-Starpanthfind;2023.6EnableExplicit#wd=65;width#Xc=20#Yc=20#obstruct=1#channel=0#BallsCount=10DeclareModuleLinearlySpacedValueDeclare.fFloat(IncrementID.l,IncrementMax.l,MinValue.f,MaxValue.f)EndDe......
  • 17 Two-Colored Dominoes
    多米诺骨牌在棋盘上放置多米诺骨牌,两端的颜色都不一样,要求横竖的总和是一样的......
  • Two-Colored Dominoes 题解
    前言看了这道题的几篇题解,感觉讲的方法都比较麻烦,这里讲一个感觉比较简单的方法。思路首先判断是否有解。计算一下每一行和每一列的牌的数量,只要有一个是奇数就无解,否则有解。证明显然,偶数一定可以分成两组,在纸上模拟一下也可以得出。其次看如何构造。对于竖着的牌,显然只对每......
  • ABC332G Not Too Many Balls 题解
    第\(i\)种球有\(a_i\)个,共\(n\)种。第\(i\)种箱子最多共装\(b_i\)个球。共\(m\)种。第\(i\)种球在第\(j\)种箱子里至多放\(ij\)个。问所有箱子放的球数最多是多少。\(1\leqn\leq500,1\leqm\leq5e5,0\leqa_i,b_i\leq1e12\)。很容易建出网络流模型。......
  • 【题解】AtCoder abc332_g Not Too Many Balls
    传送门:https://atcoder.jp/contests/abc332/tasks/abc332_g看完题,第一眼反应为最大流。建模方式为:以颜色为左部点,盒子为右部点,源点$S$向颜色$i$连一条容量为$A_i$的边,盒子$j$向汇点$T$连一条容量为$B_j$的边,颜色$i$向盒子$j$连一条容量为$ij$的边;在这张图......
  • coloredlogs用法
    用法下面是一个示例,说明入门是多么容易:importcoloredlogs,logging#创建一个记录器对象。logger=logging.getLogger(__name__)#默认情况下,install()函数会在根记录器上安装一个处理程序,#这意味着从代码中记录消息,您使用的库都将显示在终端上。coloredlogs.install......
  • CodeForces 1060G Balls and Pockets
    洛谷传送门CF传送门NOIP模拟赛T2。很厉害的题。想象数轴上\(a_1,a_2,\ldots,a_n\)位置上各有一个洞,每个非负整数位置上有一个点。每次操作相当于,对于每个点,如果它刚好位于一个洞,那么它会掉进去;否则设它的位置为\(p\),位置在它前面的洞有\(t\)个,那么这个点的位置变成......