[Luogu] P1374 最大约数和
Luogu P1734 最大约数和
读入S,预处理\(1\)到\(S\)之间所有数的约数和,然后直接跑一个\(O(n^2)\)的\(01\)背包即可。
#include <algorithm>
#include <cstdio>
const int MAXN = 1001;
int n, rslt;
int c[MAXN], f[MAXN];
inline int solve(int now) {
int ans(0);
for (int i = 1; i < now; ++i)
if(!(now % i)) ans = ans + i;
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
c[i] = solve(i);
for (int i = 1; i <= n; ++i)
for (int j = n; j >= i; --j)
f[j] = std::max(f[j], f[j - i] + c[i]), rslt = std::max(rslt, f[j]);
printf("%d\n", f[n]);
return 0;
}