Light oj 1233 - Coin Change (III) (背包优化)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1233

题目就不说明了。

背包的二进制优化,比如10可以表示为1 2 4 3,而这些数能表示1 ~ 10的任意的数。然后类似01背包就好了。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 typedef long long LL;
15 const int N = 1e5 + 5;
16 int dp[N];
17 int val[105], c[105];
18
19 int main()
20 {
21     int t, n, m;
22     scanf("%d", &t);
23     for(int ca = 1; ca <= t; ++ca) {
24         scanf("%d %d", &n, &m);
25         for(int i = 1; i <= n; ++i) {
26             scanf("%d", val + i);
27         }
28         for(int i = 1; i <= n; ++i) {
29             scanf("%d", c + i);
30         }
31         memset(dp, 0, sizeof(dp));
32         dp[0] = 1;
33         for(int i = 1; i <= n; ++i) {
34             for(int k = 1; k <= c[i]; k <<= 1) {
35                 for(int j = m; j >= k*val[i]; --j) {
36                     dp[j] |= dp[j - k*val[i]];
37                 }
38                 c[i] -= k;
39             }
40             if(c[i]) {
41                 for(int j = m; j >= c[i]*val[i]; --j) {
42                     dp[j] |= dp[j - c[i]*val[i]];
43                 }
44             }
45         }
46         int ans = 0;
47         for(int i = 1; i <= m; ++i) {
48             ans += dp[i];
49         }
50         printf("Case %d: %d\n", ca, ans);
51     }
52     return 0;
53 }
时间: 2024-10-03 13:45:01

Light oj 1233 - Coin Change (III) (背包优化)的相关文章

uva674 - Coin Change(完全背包)

题目:uva674 - Coin Change(完全背包) 题目大意:给1 5 10 25 50 这5中面值的硬币,然后给出N,问用这些钱组成N的不同方式数目.1 5 和 5 1 表示同一中,顺序不同算相同. 解题思路:完全背包. 状态方程:dp[j] += dp[ j - v[i]]: 代码: #include <cstdio> #include <cstring> const int N = 5; const int maxn = 8000; typedef long long

LightOJ 1231 Coin Change (I) (背包计数模板题)

1231 - Coin Change (I) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB In a strange shop there are n types of coins of valueA1, A2 ... An.C1, C2,... Cn denote the number of coins of valueA1, A2... An respectively. You have

Leetcode OJ --- 322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,

LeetCode OJ 322. Coin Change DP求解

题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accepted: 15289 Total Submissions: 62250 Difficulty: Medium You are given coins of different denominations and a total amount of money amount. Write a func

Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

light oj 1138 - Trailing Zeroes (III)(阶乘末尾0)

You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail. Input Input starts with an integer T (≤ 10000)

Light OJ Dynamic Programming

免费做一样新 1004 - Monkey Banana Problem 号码塔 1005 - Rooks 排列 1013 - Love Calculator LCS变形 dp[i][j][k]对于第一个字符串i 到jLCS为k的方案数 1068 - Investigation 数位dp 能被K整数且各位数字之和也能被K整除的数 dp[i][j][k] 到第i位每位数字之和的余数为j 当前数字余数为k 1079 - Just another Robbery 01背包 全部钱之和为背包体积 不被抓的

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given numbers