1742:Coins
Coins
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 40853 | Accepted: 13835 |
Description
People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
Input
The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0
Sample Output
8 4
Source
DP1:
dp[i + 1][j] 前 i 种硬币是否能凑成 j
dp[i + 1][j] = {存在 0 < k < C[i] 使得 dp[i][j - k*A[i]] 为真}
时间复杂度 O(nm2)
DP2:
dp[i + 1][j] 前 i 种硬币凑成 j 之后第 i 种硬币剩余的个数
if(dp[j] > -1) dp[j] = C[i]; //前 i - 1 种硬币已经凑成 j,则第 i 种硬币剩下 C[i] 个
else if(A[i] > j || dp[j-A[i]] < 0) dp[j] = -1; // 凑不成 j
else dp[j] = dp[j-A[i]] - 1; // 前 i - 1 种硬币可以凑成 j - A[i],则第 i 种硬币剩下 dp[j-A[i]] - 1 个
时间复杂度 O(nm)#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 105,maxm = 100005;
int n,m,A[maxn],C[maxn],dp[maxm];
//二维数组,内存超限
void solve(){
scanf("%d %d\n",&n,&m);
while(n && m){
for(int i=0;i<n;i++) scanf("%d",&A[i]);
for(int i=0;i<n;i++) scanf("%d",&C[i]);
dp[0][0] = 1;
for(int i=0;i<n;i++){
for(int j=0;j<=m;j++){
for(int k=0;k<=C[i] && k*A[i]<=j;k++){
dp[i+1][j] |= dp[i][j-k*A[i]];
}
}
}
int cnt = 0;
for(int i=1;i<=m;i++){
if(dp[n][i]) cnt++;
}
printf("%d\n",cnt);
memset(dp,0,sizeof(dp));
scanf("%d %d",&n,&m);
}
}
//二维数组用以为滚动数组替代,错误,因为该解法需要保存i - 1的数据,否则
//dp[j] |= dp[j-k*A[i]] 相当于 dp[i+1][j] |= dp[i][j-k*A[i]] || dp[i+1][j-k*A[i]]
//最后的结果是不对的。
void solve(){
scanf("%d %d\n",&n,&m);
while(n && m){
for(int i=0;i<n;i++) scanf("%d",&A[i]);
for(int i=0;i<n;i++) scanf("%d",&C[i]);
dp[0] = 1;
for(int i=0;i<n;i++){
for(int j=1;j<=m;j++){
for(int k=1;k<=C[i] && k*A[i]<=j;k++){
dp[j] |= dp[j-k*A[i]];
}
}
}
int cnt = 0;
for(int i=1;i<=m;i++){
if(dp[i]) cnt++;
}
printf("%d\n",cnt);
memset(dp,0,sizeof(dp));
scanf("%d %d",&n,&m);
}
}
//空间优化之后的正确程序,超时。。。
int dp[2][maxm];
void solve(){
scanf("%d %d\n",&n,&m);
while(n && m){
for(int i=0;i<n;i++) scanf("%d",&A[i]);
for(int i=0;i<n;i++) scanf("%d",&C[i]);
dp[1][0] = 1;
int mark;
for(int i=0;i<n;i++){
mark = i % 2;
for(int j=0;j<=m;j++){
for(int k=0;!dp[mark][j] && k<=C[i] && k*A[i]<=j ;k++){
dp[mark][j] |= dp[1-mark][j-k*A[i]];
}
}
}
int cnt = 0;
for(int i=1;i<=m;i++){
if(dp[mark][i]) cnt++;
}
printf("%d\n",cnt);
memset(dp,0,sizeof(dp));
scanf("%d %d",&n,&m);
}
}
//递推式优化后的程序
void solve(){
scanf("%d %d",&n,&m);
while(n && m){
for(int i=0;i<n;i++) scanf("%d",&A[i]);
for(int i=0;i<n;i++) scanf("%d",&C[i]);
memset(dp,-1,sizeof(dp));
dp[0] = 1;
for(int i=0;i<n;i++){
for(int j=0;j<=m;j++){
if(dp[j] > -1) dp[j] = C[i];
else if(A[i] > j || dp[j-A[i]] < 0) dp[j] = -1;
else dp[j] = dp[j-A[i]] - 1;
}
}
int cnt = 0;
for(int i=1;i<=m;i++){
if(dp[i] > -1) cnt++;
}
printf("%d\n",cnt);
scanf("%d %d",&n,&m);
}
}
int main(){
solve();
return 0;
}
Email:JingwangLi@outlook.com