天天看點

USACO 2.3 Money Systems (DP 動态規劃 + 空間優化)

#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9

//ways[constructedMoney]表示湊成constructedMoney的數量的錢時的方案數
long long ways[10001];

int main(){
#if DEBUG
	int testCase;
	for (testCase = 1; testCase <= TESTCASES; testCase++){
		char inputFileName[20] = "inputX.txt";
		inputFileName[5] = '1' +  (testCase - 1);
		freopen(inputFileName, "r", stdin);
		printf("\n#%d\n", testCase);
#endif

	 int typesOfCoins, moneyToConstruct;
	 scanf("%d%d", &typesOfCoins, &moneyToConstruct);
		
	 int constructedMoney;
	 for (constructedMoney = 1; constructedMoney <= moneyToConstruct; constructedMoney++)
		 ways[constructedMoney] = 0;
	 ways[0] = 1;

	 int type;
	 for (type = 1; type <= typesOfCoins; type++){
		 int coinsValue;
		 scanf("%d", &coinsValue);
		 for (constructedMoney = coinsValue; constructedMoney <= moneyToConstruct; constructedMoney++)
			 ways[constructedMoney] += ways[constructedMoney - coinsValue];
	 }

	printf("%lld\n", ways[moneyToConstruct]);

#if DEBUG
	}
#endif
	return 0;
}