Dollar Dayz POJ3181

Dollar Dayz POJ-3181 (complete backpack problem)

Dollar Dayz POJ3181 —— vjudge

This problem can be regarded as a complete knapsack problem, which can be regarded as an infinite coin bagging model.
But the data is 1 <= N <= 1000, 1 <= K <= 100. Need to combine high-precision addition calculations.
The state is expressed as f [j] and the number of coin combinations when the amount is i.
State transition f [j] = f [j-i] + f [j] the number of combinations of the current category + the number of combinations using a larger denomination

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

const int N = 1010;
int f[N][110];
void bigIntegerAdd(int x, int y)//高精度加法
{
    
    
    for (int i = 0; i < 100; i++)
    {
    
    
        f[x][i] += f[y][i];
        if (f[x][i] >= 10)
        {
    
    
            f[x][i] %= 10;
            f[x][i + 1]++;
        }
    }
}

int main()
{
    
    
    int n, k;
    cin >> n >> k;
    f[0][0] = 1;
    for (int i = 1; i <= k; i++)
    {
    
    
        for (int j = i; j <= n; j++)
        {
    
    
            bigIntegerAdd(j, j - i);
        }
    }
    int t = 100;
    while (t > 0 && f[n][t] == 0)//去除前导0
        t--;
    for (int i = t; i >= 0; i--)
    {
    
    
        cout << f[n][i];
    }
}

Guess you like

Origin blog.csdn.net/qq_45830383/article/details/108297269