D. Effects of Anti Pimples

D. Effects of Anti Pimples

Chaneka has an array [a1,a2,,an]. 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 ai out of all black and green elements.

There are 2n1 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 998244353.

Input

The first line contains a single integer n (1n105) — the size of array a.

The second line contains n integers a1,a2,a3,,an (0ai105).

Output

An integer representing the sum of scores for all possible ways Chaneka can choose the black indices, modulo 998244353.

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, 也会被染色,因此最大值的取值还要考虑到这些位置。定义数组 b,其中 bi 表示只对下标 i 染色时能取到的最大值,即 bi=max{ai,a2i,,anii}。如果某个方案中有 k 个下标 i1,,ik 染了黑色,那么其结果也就是最大值为 max1jk{bij}

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

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

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

  AC 代码如下,时间复杂度为 O(nlogn)

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

typedef long long LL;

const int N = 1e5 + 10, mod = 998244353;

int a[N], b[N];

int qmi(int a, int k) {
    int ret = 1;
    while (k) {
        if (k & 1) ret = 1ll * ret * a % mod;
        a = 1ll * a * a % mod;
        k >>= 1;
    }
    return ret;
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j += i) {
            b[i] = max(b[i], a[j]);
        }
    }
    sort(b + 1, b + n + 1);
    int ret = 0;
    for (int i = 1; i <= n; i++) {
        int j = i + 1;
        while (j <= n && b[j] == b[i]) {
            j++;
        }
        ret = (ret + (qmi(2, j - i) - 1ll + mod) * qmi(2, i - 1) % mod * b[i]) % mod;
        i = j - 1;
    }
    printf("%d", ret);

    return 0;
}

 

参考资料

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

posted @   onlyblues  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-10-09 Using a Robot to Print the Lexicographically Smallest String
2022-10-09 最小移动距离
Web Analytics
点击右上角即可分享
微信分享提示