# BZOJ 3233: [Ahoi2013]找硬币( dp )

dp(x)表示最大面值为x时需要的最少硬币数.

----------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn = 59;
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]找硬币

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 617  Solved: 275
[Submit][Status][Discuss]

2
25 102

4

## HINT

1<=N<=50, 1<=ai<=100,000

## Source

posted @ 2016-02-16 22:38  JSZX11556  阅读(368)  评论(0编辑  收藏