ABC 286 D - Money in Hand(背包)

https://atcoder.jp/contests/abc286/tasks/abc286_d

题目大意:

给定n种钱,一共需要我们凑到x元。

n种类型的钱中每一种钱币值为ai,有bi张。

问我们能不能够凑到x元?可以的话输出Yes,不行的话就输出No。
Sample Input 1  
2 19
2 3
5 6
Sample Output 1  
Yes

这里用的是最简单的二维的方法

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=4010;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL n,x;
LL a[N],b[N],f[55][10010];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>x;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i]>>b[i];
        }
        f[0][0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=x;j++)
            {
                for(int k=0;k<=b[i];k++)
                {
                    if(j-k*a[i]>=0&&f[i-1][j-k*a[i]])
                        f[i][j]=1;
                }
            }
        }
        if(f[n][x]==1) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}
posted @ 2023-03-02 17:15  高尔赛凡尔娟  阅读(47)  评论(0编辑  收藏  举报