首页 > 其他分享 >CF 788C(The Great Mixing-背包)

CF 788C(The Great Mixing-背包)

时间:2022-10-24 18:05:57浏览次数:66  
标签:Great ch nn int ll Mixing include 788C define


有k瓶饮料,碳酸含量为a_1/1000,每瓶饮料取整数分,问怎么凑出x/1000的饮料。0<=a_i<=1000

显然a1−n+a2−n+...+ak−n=0
建图,在[-1000,1000]上每个点连出k条边,求经过0点的最小环。
由于−1000<=ai−n<=1000,所以存在最小环所有点在[-1000,1000]上,bfs

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll n,m,a[1000000+10];
ll nn=1500;
int f[3000];
int main() {
cin>>n>>m;
For(i,m) a[i]=read();
sort(a+1,a+1+m);
m=unique(a+1,a+1+m)-a-1;
vi v;
MEMI(f)
For(i,m) v.pb(a[i]-n),f[a[i]-n+nn]=1;
Rep(i,SI(v)) {
For(j,m) {
int p=v[i]+a[j]-n;
if (-1001<=p&&p<=1001&& f[p+nn]==INF) {
f[p+nn]=f[v[i]+nn]+1;
v.pb(p);
}
}
}
if (f[nn]==INF) f[nn]=-1;
cout<<f[nn]<<endl;
return 0;
}


标签:Great,ch,nn,int,ll,Mixing,include,788C,define
From: https://blog.51cto.com/u_15724837/5790746

相关文章