Feast Coins(多重背包)
来源:互联网 发布:淘宝运营视频 编辑:程序博客网 时间:2024/06/05 09:17
Last feast the young princess received way too many coins. Since she is very young, she doesn’t know the value of each coin, if you give her a coin with the value 5 or a coin with the value 1, she will consider them both as just 1 coin, regardless of the value.
However, she can notice that the coin with value 5 doesn’t look the same as the coin with value 1, and she will be happy if she has the same number of coins of each value. Otherwise, she will not be happy.
She has a lot of coins of different values, and she needs some subset of these coins such that the sum of their values should be exactly S , and the number of coins of each value in this subset should be the same. Can you help her to count the number of different ways to do this?
Input
Your program will be tested on one or more test cases. The first line of the input will be a single integer T (1 ≤ T ≤ 100) representing the number of test cases. Followed by T test cases. Each test case starts with a line containing two integers separated by a single space S and N (1 ≤ S≤ 5,000) (1≤ N ≤ 50) representing the total required sum and the number of different values of coins, respectively. Followed by N lines, each one will contain two integers separated by a single space Vi and Ci (1 ≤ Vi ; Ci≤ 5,000) representing the value of a coin and the number of coins the princess has with this value, respectively. For each test case, all values of Vi will be distinct.
Output
For each test case print a single line containing “Case n:” (without quotes) where n is the test case number (starting from 1) followed by a space then the number of different ways to make the total sum S as described above. Two ways are considered different if any coin value does not appear the same number of times in both ways.
You can assume that the result will always fit in a 64-bit signed integer.
Sample Input
210 22 26 110 41 102 103 104 10Sample Output
Case 1: 0Case 2: 5Judge TipsNote In the first test case, the only way to make the sum 10, is to use the following subset of coins (2, 2, 6), but this isn’t valid because there are 2 coins with value 2 and 1 coin with value 6. In the second test case, the following are the 5 different ways: (1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (2, 2, 2, 2, 2), (2, 2, 3, 3), (1, 1, 4, 4), (1, 2, 3, 4).
本题的题意是给定目标钱数s和货币的面值种数n
每种货币又有ni个,用这些货币组成s
要求货币要么就不用,要么用的货币个数一样
思路是多重背包问题
从一个货币组成S/i 遍历所有能被s整除的i的货币。
第一次写,很多都不能明白,这里存个代码,多去写几道多重背包
#include<cstdio>#include<iostream>#include<map>#include<cstring>#include<cmath>using namespace std;#define N 10000__int64 s,dp[N],ans,v[N],c[N];int main(){int i,j,k,n,t,a,kase=0;;cin>>t;while(t--){cin>>s>>n;__int64 Max=0;for(i=1;i<=n;i++){ cin>>v[i]>>c[i]; Max=max(Max,c[i]); } ans=0; for(i=1;i<=Max&&i<=s;i++) if(s%i==0) { __int64 kk=s/i; memset(dp,0,sizeof(dp)); dp[0]=1; for(j=1;j<=n;j++) { if(c[j]>=i) { for(k=kk-v[j];k>=0;k--) dp[k+v[j]]+=dp[k]; } }ans+=dp[kk]; } cout<<"Case "<<++kase<<": "<<ans<<endl; }return 0;}
’
- Feast Coins(多重背包)
- Coins(多重背包)
- Coins(多重背包)
- hdu-Coins(多重背包)
- Coins(经典)(多重背包)
- poj1742- Coins(多重背包)
- hdu2844 Coins(多重背包)
- HDU2844-Coins(多重背包)
- HDU2844 Coins(多重背包)
- Coins (多重背包,较好的题)
- HDU 2844 Coins(多重背包)
- pku 1742 Coins(多重背包dp)
- hdu 2844 Coins (多重背包)
- Poj 1742 Coins(多重背包)
- hdu 2844 Coins(多重背包优化)
- poj 1742 Coins (多重背包)
- [ACM] hdu 2844 Coins (多重背包)
- POJ 1742 - Coins (dp 多重背包)
- iOS 中的单例模式
- IPSec-VPN 编译与部署 (Strongswan 5.4.0 )
- 结构体中动态内存的管理(malloc和free)
- SQL Server 连接字符串和身份验证
- 杭电oj 2002 计算球的体积
- Feast Coins(多重背包)
- per-CPU变量
- 持久化修改Android模拟器的system分区
- POJ - 3045/USACO - Nov05 Silver Cow Acrobats 牛的杂技 重庆一中高2018级竞赛班第四次测试 2016.7.25 Problem 3
- Postgresql ERROR: database is not accepting commands to avoid wraparound data loss in database
- 神经网络与深度学习1
- BYTE bits[1] 和 BYTE* bits的区别
- SVM如何避免过拟合
- hadoop:Streaming接口实现