Effects of Anti Pimples (Codeforces 1877D)
Tags: Others, number theory

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$$$.

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$$$.

Sample Inputs:
4
19 14 19 9

1
0

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

Sample Outputs:
265

0

266012571

Solution (Python):
x = 998244353
n = int(input())
l = list(map(int, input().split()))
m = []
ans = 0
for i in range(n):
    k = max(l[i::i + 1])
    m.append(k)
m.sort()
z = 1
for j in range(n):
    if j == 0:
        z = 1
    else:
        z = z % x * 2 % x
    ans = (ans % x + z % x * (m[j] % x) % x) % x
print(ans)

Solution (Python):
c = 998244353
n = int(input())
a = [0] + list(map(int, input().split()))
l = [max(a[i:n + 1:i]) for i in range(1, n + 1)]
l.sort(reverse=True)
ans = 0
for (i, j) in enumerate(l):
    ans += j * pow(2, n - i - 1, c)
    ans %= c
print(ans)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
for i in range(0, n):
    maxValue = a[i]
    for j in range(i, n, i + 1):
        if a[j] > maxValue:
            maxValue = a[j]
    a[i] = maxValue
a.sort()
exponent = 1
sum1 = 0
for i in range(0, n):
    value = a[i] * exponent
    value %= 998244353
    sum1 += value
    sum1 %= 998244353
    exponent *= 2
    exponent %= 998244353
print(sum1)