题目传送门
解题思路:
先O(n^2)求出小于等于S的所有整数的约数和,跑01背包
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int n,f[1001],sum[1001]; 7 8 int main() { 9 scanf("%d",&n); 10 sum[1] = 0; 11 sum[2] = 1; 12 for(int i = 3;i <= n; i++){ 13 for(int j = 2;j < i; j++) 14 if(i % j == 0) 15 sum[i] += j; 16 sum[i]++; 17 } 18 for(int i = 1;i <= n; i++) 19 for(int j = n;j >= i; j--) 20 f[j] = max(f[j],f[j-i] + sum[i]); 21 printf("%d",f[n]); 22 return 0; 23 }