POJ 3181 题目链接
题目大意:
给出价值从1-K的货物,问花完N块钱去买这些货物可以有多少种方法。
思路:
很典型的背包问题,用一维数组,不过要用到高精度计算的技巧,不然WA。
ps: 这点在interviewStreet中有用到过,当时是python的大数毫无压力秒过,但是C++要用string来模拟。
解题代码如下:
/*
* poj_3181_dollar_dayz.cpp
*
* Created on: 2013-7-7
* Author: zhengwei
*/
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 100;
const int MAX_AMOUNT = 1100;
vector > bigDpArray(MAX_AMOUNT, vector(MAX));
void computeAdd(int amount, int prevAmount) {
for (int i = 0; i <= MAX; i++) {
bigDpArray[amount][i] += bigDpArray[prevAmount][i];
if (bigDpArray[amount][i] >= 10) {
bigDpArray[amount][i] %= 10;
bigDpArray[amount][i+1]++;
}
}
}
int main () {
int totalAmount, denoNum;
while (cin >> totalAmount >> denoNum) {
fill(bigDpArray.begin(), bigDpArray.end(), vector(MAX, 0));
bigDpArray[0][0] = 1;
for (int i = 1; i <= denoNum; i++) {
for (int j = i; j <= totalAmount; j++) {
computeAdd(j, j-i);
}
}
int colNum = MAX-1;
while (bigDpArray[totalAmount][colNum] == 0 && colNum >= 0) {
colNum --;
}
for (; colNum >= 0; colNum --) {
cout << bigDpArray[totalAmount][colNum];
}
cout << endl;
}
}