DP (x) indicates the minimum number of coins required for a maximum face value of x.
The mass factor p of the enumeration x, DP (x) = Min (DP (x/p)-(p-1) * sigma[a[i]/x]).
----------------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN =;const int MAXM = 100009;int N, M, W[MAXN], DP[MAXM];int P[MAXM], MINP[MAXM], PN;bool F[MAXM];Template<class t>inline void Min (t &x, T t) {if (t < x) x = t;}Template<class t>inline void Max (t &x, T t) {if (T > x) x = t;}void Init () {scanf ("%d", &n);memset (DP,-1, sizeof DP);M = dp[1] = 0;for (int i = 0; i < N; i++) {scanf ("%d", w + i);Max (M, w[i]);dp[1] + = W[i];}memset (f, 0, sizeof f);pn = 0;for (int i = 2; I <= M; i++) {if (! F[i])Minp[i] = p[pn++] = i;For (int j = 0; J < pn && I * p[j] <= M; j + +) {f[i * P[j]] = true;minp[i * P[j]] = p[j];if (i% p[j] = = 0) break;}}}void Work () {int ans = dp[1];for (int i = 0; i < PN; i++) {Dp[p[i]] = 0;For (int j = 0; J < N; J + +)Dp[p[i] [+] + w[j]/p[i] + w[j]% p[i];Min (ans, dp[p[i]]);}for (int i = 2; I <= M; i++) if (!~dp[i]) {Dp[i] = dp[1];for (int t = i; t! = 1; t/= Minp[t]) {int v = 0;For (int j = 0; J < N; J + +)v + = w[j]/i;Min (Dp[i], dp[i/minp[t]]-V * (Minp[t]-1));}Min (ans, dp[i]);}printf ("%d\n", ans);}int main () {Init ();Work ();return 0;}
----------------------------------------------------------------------------------
3233: [Ahoi2013] Find a coin time limit: ten Sec Memory Limit: MB
Submit: 617 Solved: 275
[Submit] [Status] [Discuss] DescriptionThe Little Snake is the minister of Finance. Recently she decided to make a series of new currencies. Suppose she was to make a currency of x1,x2,x3 ... Then X1 must be a positive integer multiple (b>a) for 1,xb that must be XA. For example 1,5,125,250 is a set of valid coin sequences, and 1,5,100,125 is not. I do not know from which day, the lovely snake fell in love with a kind of Meng-Rabbit paper! From then on, the little snake went to meet the rabbit paper Doll bought the road of no return. One day, the little snake saw n cute rabbit paper, assuming that the price of the N-Rabbit paper is a1,a2...an. Now the snake wants to know the minimum number of coins needed to buy the N-Rabbit paper under a valid set of coin sequences. You can't change when you buy rabbit paper. InputThe first line, an integer n, indicates the number of rabbit paperThe second line, n spaces separated by a number of integers, respectively, the price of N rabbit paperOutput
A line, an integer that represents the minimum number of coins to pay.
Sample Input2
25 102
Sample Output4
HINT
Sample explanation: There are two rabbit paper, the prices are 25 and 102 respectively. Now the small snake structure 1,25,100 such a set of coin sequence, then pay the first rabbit paper only need a face value of 25 coins, the second rabbit paper needs a face value of 100 coins and two denominations of 1 coins, a total of two rabbit paper to pay 4 coins. This is also the minimum number of coins to be paid in all programs.
1<=N<=50, 1<=ai<=100,000
Source
Bzoj 3233: [Ahoi2013] Find a coin (DP)