D. Triangle Coloring

D. Triangle Coloring

You are given an undirected graph consisting of $n$ vertices and $n$ edges, where $n$ is divisible by $6$. Each edge has a weight, which is a positive (greater than zero) integer.

The graph has the following structure: it is split into $frac{n}{3}$ triples of vertices, the first triple consisting of vertices $1, 2, 3$, the second triple consisting of vertices $4, 5, 6$, and so on. Every pair of vertices from the same triple is connected by an edge. There are no edges between vertices from different triples.

You have to paint the vertices of this graph into two colors, red and blue. Each vertex should have exactly one color, there should be exactly $frac{n}{2}$ red vertices and $frac{n}{2}$ blue vertices. The coloring is called valid if it meets these constraints.

The weight of the coloring is the sum of weights of edges connecting two vertices with different colors.

Let $W$ be the maximum possible weight of a valid coloring. Calculate the number of valid colorings with weight $W$, and print it modulo $998244353$.

Input

The first line contains one integer $n$ ($6 le n le 3 cdot 10^5$, $n$ is divisible by $6$).

The second line contains $n$ integers $w_1, w_2, dots, w_n$ ($1 le w_i le 1000$) — the weights of the edges. Edge $1$ connects vertices $1$ and $2$, edge $2$ connects vertices $1$ and $3$, edge $3$ connects vertices $2$ and $3$, edge $4$ connects vertices $4$ and $5$, edge $5$ connects vertices $4$ and $6$, edge $6$ connects vertices $5$ and $6$, and so on.

Output

Print one integer — the number of valid colorings with maximum possible weight, taken modulo $998244353$.

Examples

input

12
1 3 3 7 8 5 2 2 2 2 4 2

output

36

input

6
4 2 6 6 6 4

output

2

Note

The following picture describes the graph from the first example test.

D. Triangle Coloring

The maximum possible weight of a valid coloring of this graph is $31$.

 

解题思路

  先考虑最大价值是多少。对于一个三元图,很明显通过染色最多只能选择$2$条边,如下图:

D. Triangle Coloring

  因此为了获得最大价值,每一个三元图都应该选择权值最大的两条边,对应的染色方案是这两条边共同的节点染红色(蓝色),另外两个节点染蓝色(红色)。

  由于共有$frac{n}{3}$个三元图,$6 mid n$,那么有$2 mid frac{n}{3}$,因此如果一个三元图为$2$红$1$蓝,那么必然会存在另外一个三元图是$2$蓝$1$红(参考上图中上下两个三元图)。因此整个图中会有$frac{n}{6}$个三元图为$2$红$1$蓝,有$frac{n}{6}$个三元图为$2$蓝$1$红。因此从整体上来看,就有$C_{frac{n}{6}}^{frac{n}{3}}$种染色方案(从$frac{n}{3}$个三元图中选择$frac{n}{6}$个染成$2$红$1$蓝)。

  再考虑每个三元图本身,很明显如果$3$条边的权值都相同,那么它本身就有$3$种染色方案。如果权值最小的边有两条(比如${ 1,1,2 }$),那么就有$2$种染色方案。其余的情况就只有一种染色方案,即选择最大边长和次大边长。记每个三元图本身的染色方案为$c_i$。

  因此根据乘法原理,总的获得最大价值的染色方案为$C_{frac{n}{6}}^{frac{n}{3}} times prodlimits_{i=1}^{n/3} c_i$。

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int mod = 998244353;
 7 
 8 int qmi(int a, int k) {
 9     int ret = 1;
10     while (k) {
11         if (k & 1) ret = 1ll * ret * a % mod;
12         a = 1ll * a * a % mod;
13         k >>= 1;
14     }
15     return ret;
16 }
17 
18 int C(int a, int b) {
19     int ret = 1;
20     for (int i = 1, j = a; i <= b; i++, j--) {
21         ret = 1ll * ret * j % mod * qmi(i, mod - 2) % mod;
22     }
23     return ret;
24 }
25 
26 int main() {
27     int n;
28     scanf("%d", &n);
29     int ret = C(n / 3, n / 6);
30     for (int i = 0; i < n; i += 3) {
31         int a[3];
32         for (int j = 0; j < 3; j++) {
33             scanf("%d", a + j);
34         }
35         sort(a, a + 3);
36         if (a[0] == a[2] && a[1] == a[2]) ret = 3ll * ret % mod;    // 3条边的权值相等 
37         else if (a[0] == a[1]) ret = 2ll * ret % mod;    // 否则3条边的权值不完全相等,而最小的两条边相等 
38     }
39     printf("%d", ret);
40     
41     return 0;
42 }

 

参考资料

  Educational Codeforces Round 143 D(组合数学):https://zhuanlan.zhihu.com/p/607034527

  Educational Codeforces Round 143 Editorial:https://codeforces.com/blog/entry/112963

原文链接: https://www.cnblogs.com/onlyblues/p/17132913.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    D. Triangle Coloring

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/319008

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月24日 下午3:07
下一篇 2023年2月24日 下午3:08

相关推荐