D. Effects of Anti Pimples

发布时间 2023-10-09 18:27:09作者: 不o凡

D. Effects of Anti Pimples

对于样例一:
14出现2次
9出现1次
19出现12次

规律:
1.我们发现1与后面的组合的最大值等于数列的最大值,次数是2^(n-1),这是巧合吗?
2.往下递推,我们可知2与后面的组合为2的倍数的最大值,次数为2^(n-2),...
3.因此我们可以先算出每个位置的最大值,然后乘以相应的次数

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 2e5 + 10,mod= 998244353;
int a[N],b[N];
LL _pow(LL a, int b) {
	LL ans = 1;
	while (b) {
		if (b & 1) ans = (ans * a) % mod;
		a = 1LL*a * a % mod;
		b >>= 1;
	}
	return ans;
}
void solve() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> 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 + 1 + n);
	LL ans = 0;
	for (int i = 1; i <= n; i++) {
		ans = (ans +b[i]*_pow(2,i-1)) % mod;
	}
	cout << ans;
	cout << '\n';
}
int main() {
	int t=1;
	//cin >> t;
	while (t--) {
		solve();
	}
}