The Great Mixing CodeForces - 788C (BFS)

直流耦合系统介绍 参考《SMA如何使用直流耦合技术实现更高效的光伏电站-白皮书》 阅读详情


The Great Mixing

CodeForces - 788C

Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.

Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.

Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.

Input

The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.

The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.

Output

Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.

Example
Input
400 4
100 300 450 500
Output
2
Input
50 2
100 25
Output
3
Note

In the first sample case, we can achieve concentration using one liter of Coke of types and : .

In the second case, we can achieve concentration using two liters of type and one liter of type: .

给你k个不同浓度(浓度可能重复哦)的饮料,再给你一个目标浓度n,只允许让你用已给的k种饮料,每个只允许用整数升,问你最少用多少升饮料就能配成目标浓度。

首先我们发现如果要满足条件,我假设第i种饮料取ki升,则

这样我们对于每个a[i],我们a[i]-=n,然后问题转换成了我们在这个数中选数,每个数能选多次,最后让所有选出的数的sum=0

然后用一个bfs去找次数的最小值就可以了,因为数值会出现负数,所以整体加上1000,这样可以标记

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 1005
#define mask 1002
using namespace std;
int k,n,a[N*N],q[N*N];//a数组记录初始浓度,q是队列
int vis[2*N],f[2*N];//vis用于标记,f记录次数
int main(){
    scanf("%d%d",&n,&k);
    int i;
    for(i = 1; i <= k; i++)
        scanf("%d",&a[i]);
    sort(a+1,a+1+k);
    k = unique(a+1,a+1+k)-a-1;//unique函数返回第一个重复元素的迭代器,前面是非重复的
    for(i = 1; i <= k; i++)
        a[i] -= n;
    if(a[1]*a[k]>0){
        printf("-1\n");
        return 0;
    }
    int s = 1,t = 1;
    for(i = 1; i <= k; i++){
        q[t++] = a[i];
        f[a[i]+mask] = 1;
        vis[a[i]+mask] = 1;
    }
    while(s!=t&&!vis[mask]){
        for(i = 1; i <= k; i++){//枚举每个数,如果加上不够1002(实际是不到0),并且未访问过,入队 
            if(abs(a[i]+q[s])<=1002&&!vis[a[i]+q[s]+mask]){
                f[a[i]+q[s]+mask] = f[q[s]+mask]+1;//上一个状态的选取次数+1
                vis[a[i]+q[s]+mask] = 1;
                q[t++] = a[i]+q[s];
            }
        }
        s++;
    }
    printf("%d\n",f[mask]);
    return 0;
}


CarMaker快速入门 本合集是小明师兄推出的CarMaker汽车仿真,CarMaker无论是传统动力学、动力系统开发、底盘系统开发还是目前智能驾驶辅助系统及智能驾驶系统开发有着非常重要的作用,可以有效节省开发周期和开发成本,基于仿真快速实现算法开发和系统验证。... 阅读详情

相关推荐

别慌!Oracle MOS登录页面大变样?5分钟搞定新旧账号登录全流程

本文详细解析Oracle MOS登录页面改版后的新旧账号登录流程,帮助用户快速适应账号体系变革。针对传统MOS账号和OCI账号的不同应用场景,提供5分钟内完成无缝切换的实用指南,包括登录步骤、书签设置和密码管理策略,助力数据库管理用户高效访问Oracle技术支持资源。

weixin_30275415的博客 517

(codeforces) The Great Mixing

题目:Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of S

china震震的博客 723

1panel面板实战:Docker容器存储路径迁移与优化指南

本文详细介绍了在1panel面板中迁移Docker容器存储路径的完整流程与优化方法。针对系统盘空间不足的常见问题,通过停服务、搬数据、改配置三个核心步骤,指导用户安全地将Docker数据目录迁移至更大容量的独立分区,并提供了迁移后的深度管理技巧与常见避坑指南,帮助用户优化服务器存储资源。

ol789012的博客 865

codeforces Codeforces Round #407 Div2题解

5A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Anastasia loves going for a wal

blood_in_the_sky的专栏 578

ORB-SLAM3在windows11下的编译使用

近期在学习SLAM,想部署一下ORB-SLAM3,但是自己电脑是win11系统,因此就想着在win11上部署一下。但是网上看了一些教程,有一些博客,但是可能不适合我这种情况把,就很纠结。先说下结果,配置成功,过程极其痛苦。

weixin_44457768的博客 6096

K-means算法代码实现(python)

K-means算法代码实现以及解决质心选择问题 计算距离 距离通常使用欧几里得距离来衡量 def euclDistance(vector1, vector2): return np.sqrt(sum((vector2 - vector1) ** 2)) 初始化质心 def initCentroids(data, k): numSamples, dim = data.shape ...

江南小帅瓜的博客 1万+

Codeforces 788C 想法+bfs

The Great Mixing time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Sasha and Kolya decided to get drunk with C

black_miracle的博客 913

Android14 有线网络静态IP配置:从固件烧录到系统默认设置

本文详细解析了在Android14系统中,如何通过修改系统框架层和设置应用,为设备预设有线网络静态IP地址,实现固件烧录后即具备指定网络配置的功能。文章深入探讨了从扩展SettingsProvider数据库字段、配置默认值,到在设置应用中读取和应用配置的完整流程,旨在解决工业平板、智能终端等设备在无DHCP网络环境中批量部署的痛点,提升开箱即用体验。

lake5的博客 919

(CF 788)C. The Great Mixing <BFS>

题目链接: http://codeforces.com/problemset/problem/788/CC. The Great Mixingtime limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Sasha and Kolya decid

STILLxjy 1106

【Kubernetes】Kubernetes 安全沙箱深度对比:gVisor vs Kata Containers

Kubernetes安全沙箱深度对比:gVisor和Kata Containers在架构、性能与安全性方面各有特点。gVisor通过用户态内核(Sentry)实现软件隔离,启动快(毫秒级)但CPU开销大(20-30%),适合Web服务等场景。Kata Containers采用轻量级虚拟机提供硬件级隔离,启动较慢(1-2秒)但兼容性更好,适合金融等高安全需求。测试显示gVisor在动态请求下性能下降33%,而Kata性能接近原生。建议根据场景选择:多租户/快速启动用gVisor,敏感数据/极致安全用Kata,

九师兄 178

【回答问题】ChatGPT上线了!如何安装python-ipopt?python-ipopt有哪些用法?

python-ipopt是一种基于Python语言的求解非线性最优化问题的软件包。它使用了Interior Point Optimizer(IPOPT)算法来求解最优化问题。这段代码使用python-ipopt求解了一个无约束的非线性最小化问题,其目标函数为(x[0] - 1) ** 2 + (x[1] - 2) ** 2,求解结果为x_opt。求解线性规划问题、二次规划问题、二次范数最小化问题等类型的最优化问题。对于给定的最优化问题,可以设置不同的求解参数来控制求解的精度和效率。

源代码杀手的博客 2146

Codeforces 789E The Great Mixing【Bfs+dp】

E. The Great Mixing time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Sasha and Kolya decided to get drunk with Coke, ag

mengxiang000000 1376

【YOLOv11】014、YOLOv11模型导出:PyTorch到ONNX格式转换与优化

摘要:本文详细介绍了如何将PyTorch训练的YOLOv11模型高效转换为ONNX格式。重点包括:分离模型特征提取与后处理、设置动态维度、优化ONNX算子、验证转换正确性等关键步骤。作者分享了实际项目中的经验教训,如版本对齐、算子兼容性处理、后处理分离等实用技巧,并推荐使用onnx-simplifier工具优化模型。文章强调模型转换是一个需要反复调试的过程,建议保留转换记录形成知识库,以提高部署效率并避免常见错误。

企鹅码农的专属播客,分享10多年的实战案例和经验解读,包含YOLO、RT-DETR、AI等各个工作领域。 305

BFS (图)——Codeforces 788 C. The Great Mixing

题目 **The Great Mixing** Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . To...

点滴 678

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

codeforces 788C The Great Mixing (bitset优化dp、bfs)

题目原文 http://codeforces.com/problemset/problem/788/CSasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentra

wchhlbt的博客 1757

codeforces 788C The Great Mixing( BFS / dp+bitset优化 )

题意:n(1e3),k(1e6),a1~ak(1e3)。选取最少的ai(可以重复)t个,使得sum(ai)/t/1000==n/1000成立。 题解: k(1e6),a1~ak(1e3) --> 数只有1e3个。 sum(ai)/t/1000==n/1000 --> sum(ai)==t*n --> sum(ai-n)==0 tn,t=abs(ai)+abs(aj) 解法一:BFS

658

Codeforces.788C.The Great Mixing(bitset DP / BFS)

题目链接 \(Description\) 有k种饮料,浓度Ai给出,求用最少的体积配成n/1000浓度的饮料。 \(Solution\) 根据题意有方程 (A1x1+A2x2+...+Anxn)/[(x1+x2+...+xn)*1000] = x/1000 xi表示Ai选多少升(0<=xi).1000可以约掉. 这样带着带着选与不选不好考虑(我为什么会这么考虑。。太naive了) ai表示选...

weixin_30413739的博客 206

Codeforces 788C The Great Mixing(背包问题建模+bitset优化或BFS)

【题目链接】http://codeforces.com/problemset/problem/788/C 【题目大意】   给出一些浓度的饮料,要求调出n/1000浓度的饮料,问最少需要多少升饮料 【题解】   设浓度为a,现在要求出系数x1,x2,x3……,使得x1*a1+x2*a2+x3*a3+……=n*(x1+x2+x3+……)  得a1*(x1-n)+a2*(x...

weixin_30426879的博客 196

[BFS] Codeforces Round #407 (Div. 1) 788C. The Great Mixing

题意戳这里题解就是给出一些数,求取最少的数使得平均数等于m。每种数无限个。用平均数的套路,吧所有数减m,题面转换成取最少的数使得加和为 00 . 然后怎么做呢?考虑答案有多大,可以发现我们随便选 ai<0<aja_i<0<a_j,有 ai∗|aj|+aj∗|ai|=0a_i*|a_j|+a_j*|a_i|=0 ,|ai|+|aj||a_i|+|a_j| 就是一个解。所以答案一定不超过 2000200

Lynstery's blog 318

【Codeforces 788C】The Great Mixing

http://codeforces.com/contest/788/problem/C 显然如果有两杯一样的酒,把它们当作同一杯就好了。所以k<=1e6毫无意义。 若选的x杯酒的浓度分别为a,b,c,..,z,则由题意得(a+b+c+...+z)/x=n*x,整理可得(a-n)+(b-n)+(c-n)+...+(z-n)=0。 于是问题就转化成了从所有ai-n中选x个数使得和...

anlongshi2245的博客 256

CodeForces - 788C The Great Mixing

Solution 智商告吹。 首先有个技巧:kkk 为 a[i]a[i]a[i] 的平均数时,每个 a[i]a[i]a[i] 减去一个 kkk,再查看能否等于 000。(这样能够避免统计 a[i]a[i]a[i] 的个数) 然后大力 BFS\mathtt{BFS}BFS,每次都拓展一次,所以保证第一次是最优的。 Code #include <queue> #include <cstdio> #include <cstring> using namespace std;

Oxide‘s Blog 415

Codeforces 788C The Great Mixing

The Great Mixing 化简一下公式后发现, 问题变成了, 取最少多少数能使其和为1, bitset优化一下背包就好啦。 题解中介绍了一种bfs的方法没, 感觉比较巧妙。 #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak...

weixin_30878361的博客 121

2200+专项:C. The Great Mixing(bfs 思维 消去不定项)

原题: http://codeforces.com/problemset/problem/788/C 题意: 有k种饮料,其含碳量为ai/1000a_i/1000ai​/1000,每种饮料无限供应。现在要求每种饮料使用整数升调制成n/1000n/1000n/1000的饮料,问最小的使用升数。(调制公式为a1+a2+..am1000m=n1000\dfrac{a_1+a_2+..a_m}{1000m...

QAQ 468

Codeforces Round #407 (Div. 1)C. The Great Mixing

CF

m0_61104183的博客 281

Codeforces Round #407 (Div. 2)-E-The Great Mixing-滚动数组或者dfs

给定一种浓度的啤酒,给你个数字n,下面是n种浓度的啤酒,让你配成这种浓度的啤酒,问你最少需要多少份原料啤酒。 因为浓度范围很大,1后面6个零,背包就算了。范围太大。 方法1 用bitset实现滚动数组,然后用位运算可以完全模拟配酒的过程(关键在那个或),并且位运算很快,不会超时。 方法2 用BFS,要注意用map来记录他们出现的次数,这个和第一种思路差不多,就是实现方法不太一样,还有就是需要用

天与云与山与水 643
上一篇: 黄金分割数
下一篇: 剪花布条
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值