Pagini recente » Cod sursa (job #820795) | Cod sursa (job #801240) | Cod sursa (job #348606) | Cod sursa (job #2935850) | Cod sursa (job #37952)
Cod sursa(job #37952)
#include <stdio.h>
#include <stdlib.h>
#define MAXMONZ 31
struct tmoneda {
int nume;
long long valu;
long long cant;
long long solu;
};
int n, c;
long long l;
tmoneda moneda[MAXMONZ];
long long lldpow(int c, long long pw) {
long long x=1;
while (pw>0) {
x*=c;
pw--;
};
return x;
};
int SortMoneziDesc (const void * a, const void * b) {
tmoneda * aa=(tmoneda *)a;
tmoneda * bb=(tmoneda *)b;
if ((aa->valu) > (bb->valu)) return -1;
else if ((aa->valu)==(bb->valu)) return 0;
else return 1;
};
int SortMoneziNume (const void * a, const void * b) {
tmoneda * aa=(tmoneda *)a;
tmoneda * bb=(tmoneda *)b;
if ((aa->nume) > (bb->nume)) return 1;
else if ((aa->nume)==(bb->nume)) return 0;
else return -1;
};
//long long sol[MAXMONZ];
long long sumcur;
long long cantotal;
int wooh (int x) {
if (sumcur==l) {
// printf("Am gasit solutie!!\n");
// for (int i=1; i<=n; i++) {
// printf("%d: %d\n",moneda[i].nume,moneda[i].solu);
// };
return 1;
} else if (x>n) {
return 0;
} else {
long long cantcur=(l-sumcur)/(moneda[x].valu);
if (cantcur>moneda[x].cant) cantcur=moneda[x].cant;
char finisi=0;
while (((1-finisi))&&(cantcur>=0)) {
moneda[x].solu=cantcur;
sumcur+=cantcur*moneda[x].valu;
cantotal+=cantcur;
finisi=wooh(x+1);
if (!finisi) {
cantotal-=cantcur;
sumcur-=cantcur*moneda[x].valu;
moneda[x].solu=0;
};
};
return finisi;
};
};
int main (void) {
FILE * fi = fopen("shop.in","rt");
FILE * fo = fopen("shop.out","wt");
fscanf(fi,"%d %d %lld",&n,&c,&l);
long long temp;
for (int i=1; i<=n; i++) {
fscanf(fi,"%lld %lld",&temp,&moneda[i].cant);
moneda[i].valu=lldpow(c,temp);
moneda[i].nume=i;
};
qsort(moneda+1,n,sizeof(tmoneda),SortMoneziDesc);
/* tmoneda aux;
for (int i=1; i<=n/2; i++) {
aux=moneda[i];
moneda[i]=moneda[n-i+1];
moneda[n-i+1]=aux;
};*/
// printf("Valori monezi\n"); for (int i=1; i<=n; i++) printf("%lld\n",moneda[i].valu); printf("\n");
wooh(1);
qsort(moneda+1,n,sizeof(tmoneda),SortMoneziNume);
//debug
/* for (int i=1; i<=n; i++) {
fprintf(stderr,"%lld ",moneda[i].valu);
}; fprintf(stderr,"\n");*/
fprintf(fo,"%lld\n",cantotal);
fprintf(fo,"%lld",moneda[1].solu);
for (int i=2; i<=n; i++) {
fprintf(fo," %lld",moneda[i].solu);
};
fprintf(fo,"\n");
fclose(fi); fclose(fo);
return 0;
};