POJ-3181 Dollar Dayz 【完全背包】

版权声明:本文为博主原创文章,转载请注明出处( • ̀ω•́ )✧ https://blog.csdn.net/wangws_sb/article/details/83757304

题目传送门

题目:输入两个数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;
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/83757304
今日推荐