Coins
Coins
多重背包可行性
SCUACM2022集训前训练-动态规划 - Virtual Judge (vjudge.net)
本题若用二进制拆解多重背包会T,可用单调队列优化
但由于本题是求可行性而非最优化,可用进行剪枝来减小复杂度
: 能否被表示出来
:当前这种货币,表示到 块钱,已经用了多少张
可第一层循环枚举货币种类,第二层枚举要表示的面值
这三种情况就不用判断了( 块钱已经能被表示了; 都没法被表示,加一个 也不行;表示 已经花光了这种货币)
f[j] || !f[j-a[i]] || used[j-a[i]] >= c[i]
复杂度
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e2 + 10, M = 1e5 + 10;
int a[N], c[N];
int used[M];
bool f[M];
int n, m;
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
while(cin >> n >> m, n || m)
{
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> c[i];
memset(f, false, sizeof f);
f[0] = true;
for (int i = 1; i <= n; i++)
{
memset(used, 0, sizeof used);
for (int j = 1; j <= m; j++)
{
if (f[j] || !f[j-a[i]] || used[j-a[i]] >= c[i])
continue;
f[j] = true;
used[j] = used[j-a[i]] + 1;
}
}
int ans = 0;
for (int i = 1; i <= m; i++)
ans += f[i];
cout << ans << endl;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南