D. Effects of Anti Pimples
D. Effects of Anti Pimples
Chaneka has an array . 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 out of all black and green elements.
There are 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 .
Input
The first line contains a single integer () — the size of array .
The second line contains integers ().
Output
An integer representing the sum of scores for all possible ways Chaneka can choose the black indices, modulo .
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 possible ways to choose the black indices:
- Index is black. Indices , , and are green. Maximum value among them is .
- Index is black. Index is green. Maximum value among them is .
- Index is black. Maximum value among them is .
- Index is black. Maximum value among them is .
- Indices and are black. Indices and are green. Maximum value among them is .
- Indices and are black. Indices and are green. Maximum value among them is .
- Indices and are black. Indices and are green. Maximum value among them is .
- Indices and are black. Index is green. Maximum value among them is .
- Indices and are black. Maximum value among them is .
- Indices and are black. Maximum value among them is .
- Indices , , and are black. Index is green. Maximum value among them is .
- Indices , , and are black. Index is green. Maximum value among them is .
- Indices , , and are black. Index is green. Maximum value among them is .
- Indices , , and are black. Maximum value among them is .
- Indices , , , and are black. Maximum value among them is .
The total sum is .
解题思路
如果要将下标 染色,那么所有是 的倍数的下标 也会被染色,因此最大值的取值还要考虑到这些位置。定义数组 ,其中 表示只对下标 染色时能取到的最大值,即 。如果某个方案中有 个下标 染了黑色,那么其结果也就是最大值为 。
我们不可能把 种方案全部枚举出来,意味着可以根据某个属性来对这些方案进行分类。可以发现所有方案的不同结果最多只有 种,也就是不同 的数量,因此我们可以根据方案的结果也就是最大值来进行分类,那么最多会有 类,然后再通过组合计数来统计每一类贡献的答案是多少。
考虑所有结果是 的方案,意味着至少要染一个 的下标,且不能染 的下标(否则结果必然大于 )。假设 的下标有 个, 的下标有 个,那么结果是 的方案的数量就是 。其中 是指在 个 的下标的所有染色方案中,除去均不染色的情况。 是指 个 的下标中的所有染色方案,这些下标是否染色都不会影响其结果是 。所以这些方案对答案的贡献就是 。
为此我们可以从小到大枚举 ,然后按照上面的方法对不同的 分别统计答案即可。
AC 代码如下,时间复杂度为 :
#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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17751311.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的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 最小移动距离