1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <stdio.h> #define MAXN 11
int N, ans[MAXN];
void print_ans(){ int i, j, sum=0, x[MAXN];
for(i = 1; i <= N; i++){ x[i] = 0; for(x[i] = 0, j = i; j<=N; j++) x[i] += ans[j]; sum += x[i]; }
printf("%d\n", sum); for(i = 1; i <= N; i++) printf("%d ",x[i]); printf("\n"); }
int dp(int C[], int V, int n){
int i,j;
if(V % C[n] == 0){ ans[n] += V/C[n]; print_ans(); return 1; }
for(i=n; i>0; i--){
int cont = V/C[i];
for(j=cont; j>0; j--){
ans[i] += j; if(dp(C, V-C[i]*j, n-1)) return 1; ans[i] -= j; } } return 0; }
void solve(int coin[], int m, int num){ int i, j, tmp[MAXN];
tmp[0] = 0; for(i = num; i > 0; i--){ tmp[i] = 0;
for(j =0; j < i; j++) tmp[i] += coin[j]; ans[i] = 1; m -= tmp[i]; }
if(m < 0){ printf("NA\n"); }else{
if(!dp(tmp, m, num)) printf("NA\n"); } }
int main(){
int i, m, coin[MAXN];
FILE *fin = fopen("input.txt", "r");
while(fscanf(fin, "%d %d", &m, &N) != EOF){
for(i=0; i<N; i++) fscanf(fin, "%d", &coin[i]);
solve(coin, m, N);
}
return 0; }
|