题目传送门
sol:每个下标都有选和不选两种情况,所以总方案数是$2^{n}$,在$n$最大是$100000$的情况下不符合要求。可以这样想,假设$i^{p}=k$有符合题目要求的解,还有一个整数$j$,$j$不是$i$的整次幂,$i$也不是$j$的整次幂,那么$j^{p}=k$不可能成立,所以我们可以把所以底数分开考虑,所以$2$的整次幂拉出来爆搜一次;所以$3$的整次幂拉出来爆搜一次;$4$因为在爆搜$2$的时候考虑过,所以跳过。。。最后加上$a[1]$就是答案了。
- 分类讨论爆搜
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; const int MAXN = 1e5 + 10; int a[MAXN], b[MAXN]; bool vis[MAXN]; int p[30], v[30], tot; LL dfs(int i, LL k) { if (i > tot) return k; LL res = dfs(i + 1, k); // 不取第i个 k += a[p[i]]; k -= 1LL * v[i] * b[p[i]]; for (int j = i; j <= tot; j += i) v[j] ++; res = max(res, dfs(i + 1, k)); // 取第i个 for (int j = i; j <= tot; j += i) v[j] --; return res; } 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++) scanf("%d", &b[i]); LL res = a[1]; for (int i = 2; i <= n; i++) { if (vis[i]) continue; tot = 0; for (LL j = i; j <= n; j *= i) { p[++tot] = j; vis[j] = true; } res += dfs(1, 0); } printf("%lld\n", res); return 0; }