1. 程式人生 > >POJ-3181 Dollar Dayz 【完全揹包】

POJ-3181 Dollar Dayz 【完全揹包】

題目傳送門

題目:輸入兩個數n,k,存在面值為1~k的硬幣無窮多個,求這些硬幣組合成總價值為n組合,問有多少種組合方式。

題解:完全揹包(揹包這一塊我現在也不是很清楚(▼ヘ▼#))。因為資料範圍超過了long long,可以用兩個long long 陣列去存。

AC程式碼:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define inf 0x3f3f3f
typedef long long ll;
const ll mod=1e18;
const int maxn=1010;
int n,k;
ll a[maxn],b[maxn];
int main()
{
    io;
    cin>>n>>k;
    ms(a);
    ms(b);
    b[0]=1;
    for(int i=1;i<=k;i++)
    {
        for(int j=i;j<=n;j++)
        {
            a[j]=a[j]+a[j-i]+(b[j]+b[j-i])/mod;
            b[j]=(b[j]+b[j-i])%mod;
        }
    }
    if(a[n]==0)
        cout<<b[n]<<endl;
    else
        cout<<a[n]<<b[n]<<endl;
    return 0;
}