【DP】17.2.7 T4 凑硬币 题解

来源:互联网 发布:电子商务淘宝工作 编辑:程序博客网 时间:2024/05/17 03:48

这里写图片描述
这里写图片描述

f[i][j]代表硬币面值为i的情况下j能否被凑出来。
方程:if(f[j][t]) f[j+c[i]][t+c[i]] = 1; f[j+c[i]][t] = 1;

附AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <string>#include <iomanip>#include <ctime>#include <climits>#include <cctype>#include <algorithm>#define LL long long#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;const int maxn = 505;int n,k,cnt;int a[maxn];bool f[maxn][maxn];int main() {    freopen("coin.in","r",stdin);    freopen("coin.out","w",stdout);    scanf("%d%d",&n,&k);    for(int i = 1; i <= n; i++) scanf("%d",&a[i]);    f[0][0] = 1;    for(int i = 1; i <= n; i++)        for(int j = k-a[i]; j >= 0; j--)            for(int t = j; t >= 0; t--)                if(f[j][t]) f[j+a[i]][t+a[i]] = f[j+a[i]][t] = 1;    for(int i = 0; i <= k; i++) if(f[k][i]) cnt++;    cout << cnt << endl;    for(int i = 0; i <= k; i++) if(f[k][i]) cout << i << ' ';    return 0;}
0 0
原创粉丝点击