Time Limit: 3000MS
Memory Limit: 30000K
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.
Input
The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000) 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) A 1 , A 2 , A 3... A n , C 1 , C 2 , C 3... C n ( 1 <= A i <= 100000 , 1 <= C i <= 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
题意:
有
n
n
种纸币,第种面值是
Ai
A
i
,拥有的数量是
Ci
C
i
,问能配出
1到m
1
到
m
的中的几种面值。
第一行输入两个个数
n,m
n
,
m
第二行输入2n个数,前n个是
A1...An
A
1...
A
n
,后n个是
C1...Cn
C
1...
C
n
题解:
楼教主男人八题之一….
f[j]
f
[
j
]
表示面值
j
j
是否能取到。
表示用第
i
i
种纸币取到j的面值至少需要多少张
则转移方程转化为
if(!f[j] && f[j-a[i]] && g[i][j-a[i]] < c[i]){
f[j]=1;
g[i][j]=g[i][j-a[i]]+1;
}
数组可以压掉 i i <script type="math/tex" id="MathJax-Element-16">i</script>的那一维,每次做完记得要清零
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#define LiangJiaJun main
using namespace std;
int n,m,a[104],c[104];
int f[100004],g[100004];
int w33ha(){
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&c[i]);
f[0]=1;
int ans=0;
for(int i=1;i<=n;i++){
memset(g,0,sizeof(g));
for(int j=a[i];j<=m;j++){
if(!f[j]&&f[j-a[i]]&&g[j-a[i]]<c[i]){
f[j]=1;
g[j]=g[j-a[i]]+1;
}
}
}
for(int i=1;i<=m;i++)ans+=f[i];
printf("%d\n",ans);
return 0;
}
int LiangJiaJun(){
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)break;
w33ha();
}
return 0;
}