"The main topic"
Select and not exceed several different positive integers of s so that the sum of all the numbers (excluding itself) is the maximum.
Enter a positive integer s.
Outputs the sum of the largest approximations.
Sample input to sample
11
Sample output Sample Outputs
9
Sample Description
Taking the numbers 4 and 6, you can get the maximum value (1+2) + (1+2+3) = 9.
Data size for 30% of data, s≤10;
For 100% of data, s≤1000.
Ideas
Water problem, Ordinary 01 knapsack problem, the only thing to note is that1 of all the sum is 0! I started off with a mistake because 1 didn't judge alone.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 using namespacestd;6 Const intmaxn= ++ -;7 ints;8 intW[MAXN],F[MAXN];9 Ten intMain () One { AFreopen ("mr359.in7","R", stdin); -Freopen ("Mr359.ou7","W", stdout); -scanf"%d",&s); theMemset (W,0,sizeof(w)); -w[1]=0; - for(intI=2; i<=s;i++) - { + for(intj=1; j<=sqrt (i); J + +) - if(i%j==0) + { Aw[i]+=J; at if(J*j!=i && j!=1) w[i]+=i/J; - } - } - -f[0]=0; - for(intj=1; j<=s;j++) f[j]=-0x7fffffff; in for(intI=0; i<=s;i++) - for(intj=s;j>=i;j--) to { +F[j]=max (f[j],f[j-i]+w[i]); - } thecout<<f[s]<<Endl; * return 0; $}
"Dynamic planning" mr359-the sum of greatest common divisor