Codeforces 789e The Great Mixing (bitset dp 数学)
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.
The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 1e6) — 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.
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration
, or -1 if it is impossible.
400 4
100 300 450 500
2
50 2
100 25
3
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
这像不像一个背包问题?但是我们注意到n的范围是1e6,那么如果ans存在,ans最大是多少呢? 1000!
因为假设目标浓度为b,有小于b的浓度a,大于b的浓度c。我们只需要(c-b)升a与(b-a)升c就能配成b! [(c-b)*a+(b-a)*c]/[(c-b)+(b-a)]=b
此时我们用了(c-b)+(b-a)=a-c升,最大就是1000了
现在大体思路已经确定,两层for循环,外层循环升(1~1005),内层循环a[i]。此时复杂度是1e9。
我们这时用bitset数组优化。bitset是一堆0 1 的数组,在进行位运算时很快,因为只有0 1 可以用来标记状态的有无。
我们开一个2*2000的bitset,我们将0这个位置向左移动1000位来避免负数。每次更新dp[t]数组就行了。
我代码里面 n k的定义是反着的...
代码如下:
#include <bits/stdc++.h> using namespace std;
const int maxn=1e6+;
int n,k;
int a[maxn];
bitset<> dp[];
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&k,&n)){
for (int i=;i<n;++i)
scanf("%d",&a[i]),a[i]-=k;
sort(a,a+n);
n=unique(a,a+n)-a;//将a去下重
int t=;
dp[][]=;//将初始位置标记为1,即可以被vis到
for (int i=;i<=;++i){//循环升(步数)
for (int j=;j<n;++j){
if (a[j]<)//(如果a[j]<0)
dp[t]|=dp[-t]>>(-a[j]);//将当前状态向右移-a[j]位,让下一状态继承这个变化
else
dp[t]|=dp[-t]<<(a[j]);//将当前状态向左移a[j]位
}
if (dp[t][]!=){//如果此时回到了初始状态,找到了答案
cout<<i<<endl;
return ;
}
t=-t;
dp[t].reset();//每次将上一状态清空,用滚动数组
}
printf("-1\n");
}
return ;
}
Codeforces 789e The Great Mixing (bitset dp 数学)的更多相关文章
- Codeforces.788C.The Great Mixing(bitset DP / BFS)
题目链接 \(Description\) 有k种饮料,浓度Ai给出,求用最少的体积配成n/1000浓度的饮料. \(Solution\) 根据题意有方程 (A1x1+A2x2+...+Anxn)/[( ...
- codeforces 2B The least round way(DP+数学)
The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...
- CodeForces 55D Beautiful numbers(数位dp+数学)
题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道 ...
- codeforces 264 B. Good Sequences(dp+数学的一点思想)
题目链接:http://codeforces.com/problemset/problem/264/B 题意:给出一个严格递增的一串数字,求最长的相邻两个数的gcd不为1的序列长度 其实这题可以考虑一 ...
- Codeforces 788C The Great Mixing
The Great Mixing 化简一下公式后发现, 问题变成了, 取最少多少数能使其和为1, bitset优化一下背包就好啦. 题解中介绍了一种bfs的方法没, 感觉比较巧妙. #include& ...
- Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包
PolandBall and Gifts 转换成置换群后, 对于最大值我们很好处理. 对于最小值, 只跟若干个圈能否刚好组能 k 有关. 最直观的想法就是bitset优化背包, 直接搞肯定T掉. 我们 ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor
E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...
随机推荐
- orcale获取表、字段信息
获取表字段: select * from user_tab_columns where Table_Name='用户表' order by column_name 获取表注释: select * fr ...
- 网络体系之TCP/IP模型
TCP/IP参考模型是因特网使用的参考模型,这个体系结构在它的两个主要协议出现以后,被称为TCP/IP参考模型.该模型将网络协议分为四层:网络接口层.网络层.运输层.应用层. TCP/IP协议不是TC ...
- 最强DE战斗力
最强DE战斗力 时间限制: 1 Sec 内存限制: 128 MB提交: 40 解决: 14[提交][状态] 题目描述 春秋战国时期,赵国地大物博,资源非常丰富,人民安居乐业.但许多国家对它虎视眈眈 ...
- angualr项目引入容联 七陌7mroo
最近项目要求在注册页面增加客服服务浮窗,各种查找资料准备采用7moor来实现.现记录一下实现过程,便于后期查看: 引入7moor浮窗有两种方式: 1.h5方式,这种情况一般是单独打开新页面即可: 直接 ...
- LintCode之奇偶分割数组
题目描述: 我的分析:题目要求将奇数放在偶数的前面,没有要求将奇数或偶数排序,因此我可以设置两个指针,一个(i)指向数组第一个数字,另一个(j)指向数组的最后一个数字,因为奇数要放在前面,所以从后往前 ...
- Scribd每月共有超过两亿个访客、累积数亿篇以上的文件档案,Alexa全球排名200以内
目前已登上世界300大网站,每月共有超过两亿个访客.累积数亿篇以上的文件档案.透过Flash介面的阅读器-iPaper,使用者可以在网站内浏览各种文件,由于该网站是一个文件分享平台,所有的文件都是由使 ...
- T1215:迷宫
[题目描述] 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行.同时当Extense处在某 ...
- 将QTP运行时的错误截图上传到QC
Class QCImageErrorCapture Sub Class_Terminate() 'Check if the current test has failed. If failed the ...
- C++中的智能指针类模板
1,智能指针本质上是一个对象,这个对象可以像原生的指针一样使用,因为智能指 针相关的类通过重载的技术将指针相关的操作符都进行了重载,所以智能指针对象可以像原生指针一样操作,今天学习智能指针类模板,通过 ...
- 三、python之文件的处理
1.文件的读取 1.1 读取整个文件 假设我们有一个叫做“hello.txt”的文件,文件内容如下: helloWorld helloPython helloJava 在该文件中,有三行字符串,接下来 ...