D. Effects of Anti Pimples

发布时间 2023-10-09 11:41:53作者: onlyblues

D. Effects of Anti Pimples

Chaneka has an array $[a_1,a_2,\ldots,a_n]$. Initially, all elements are white. Chaneka will choose one or more different indices and colour the elements at those chosen indices black. Then, she will choose all white elements whose indices are multiples of the index of at least one black element and colour those elements green. After that, her score is the maximum value of $a_i$ out of all black and green elements.

There are $2^n-1$ ways for Chaneka to choose the black indices. Find the sum of scores for all possible ways Chaneka can choose the black indices. Since the answer can be very big, print the answer modulo $998\,244\,353$.

Input

The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the size of array $a$.

The second line contains $n$ integers $a_1,a_2,a_3,\ldots,a_n$ ($0\leq a_i\leq10^5$).

Output

An integer representing the sum of scores for all possible ways Chaneka can choose the black indices, modulo $998\,244\,353$.

Examples

input

4
19 14 19 9

output

265

input

1
0

output

0

input

15
90000 9000 99000 900 90900 9900 99900 90 90090 9090 99090 990 90990 9990 99990

output

266012571

Note

In the first example, below are the $15$ possible ways to choose the black indices:

  • Index $1$ is black. Indices $2$, $3$, and $4$ are green. Maximum value among them is $19$.
  • Index $2$ is black. Index $4$ is green. Maximum value among them is $14$.
  • Index $3$ is black. Maximum value among them is $19$.
  • Index $4$ is black. Maximum value among them is $9$.
  • Indices $1$ and $2$ are black. Indices $3$ and $4$ are green. Maximum value among them is $19$.
  • Indices $1$ and $3$ are black. Indices $2$ and $4$ are green. Maximum value among them is $19$.
  • Indices $1$ and $4$ are black. Indices $2$ and $3$ are green. Maximum value among them is $19$.
  • Indices $2$ and $3$ are black. Index $4$ is green. Maximum value among them is $19$.
  • Indices $2$ and $4$ are black. Maximum value among them is $14$.
  • Indices $3$ and $4$ are black. Maximum value among them is $19$.
  • Indices $1$, $2$, and $3$ are black. Index $4$ is green. Maximum value among them is $19$.
  • Indices $1$, $2$, and $4$ are black. Index $3$ is green. Maximum value among them is $19$.
  • Indices $1$, $3$, and $4$ are black. Index $2$ is green. Maximum value among them is $19$.
  • Indices $2$, $3$, and $4$ are black. Maximum value among them is $19$.
  • Indices $1$, $2$, $3$, and $4$ are black. Maximum value among them is $19$.

The total sum is $19+14+19+9+19+19+19+19+14+19+19+19+19+19+19 = 265$.

 

解题思路

  如果要将下标 $i$ 染色,那么所有是 $i$ 的倍数的下标 $2i, \, 3i, \ldots$ 也会被染色,因此最大值的取值还要考虑到这些位置。定义数组 $b$,其中 $b_i$ 表示只对下标 $i$ 染色时能取到的最大值,即 $b_i = \max \left\{ a_i, \, a_{2i}, \, \ldots, \, a_{\left\lfloor \frac{n}{i} \right\rfloor i} \right\}$。如果某个方案中有 $k$ 个下标 $i_1, \, \ldots, \, i_k$ 染了黑色,那么其结果也就是最大值为 $\max\limits_{1 \leq j \leq k}\{ b_{i_j} \}$。

  我们不可能把 $2^n - 1$ 种方案全部枚举出来,意味着可以根据某个属性来对这些方案进行分类。可以发现所有方案的不同结果最多只有 $n$ 种,也就是不同 $b_i$ 的数量,因此我们可以根据方案的结果也就是最大值来进行分类,那么最多会有 $n$ 类,然后再通过组合计数来统计每一类贡献的答案是多少。

  考虑所有结果是 $w$ 的方案,意味着至少要染一个 $b_i = w$ 的下标,且不能染 $b_i > w$ 的下标(否则结果必然大于 $w$)。假设 $b_i = w$ 的下标有 $c_w$ 个,$b_i < w$ 的下标有 $s$ 个,那么结果是 $w$ 的方案的数量就是 $(2^{c_w}-1) \cdot 2^s$。其中 $2^{c_w}-1$ 是指在 $c_w$ 个 $b_i = w$ 的下标的所有染色方案中,除去均不染色的情况。$2^s$ 是指 $s$ 个 $b_i < w$ 的下标中的所有染色方案,这些下标是否染色都不会影响其结果是 $w$。所以这些方案对答案的贡献就是 $(2^{c_w}-1) \cdot 2^s \cdot w$。

  为此我们可以从小到大枚举 $b_i$,然后按照上面的方法对不同的 $b_i$ 分别统计答案即可。

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

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

 

参考资料

  Codeforces Round #902 (Div. 1, Div. 2, based on COMPFEST 15 — Final Round) Editorial:https://codeforces.com/blog/entry/121200