codeforces 286 E. Ladies' Shop (FFT)
8 seconds
256 megabytes
standard input
standard output
A ladies' shop has recently opened in the city of Ultima Thule. To get ready for the opening, the shop bought n bags. Each bag is characterised by the total weight ai of the items you can put there. The weird thing is, you cannot use these bags to put a set of items with the total weight strictly less than ai. However the weights of the items that will be sold in the shop haven't yet been defined. That's what you should determine right now.
Your task is to find the set of the items' weights p1, p2, ..., pk (1 ≤ p1 < p2 < ... < pk), such that:
- Any bag will be used. That is, for any i (1 ≤ i ≤ n) there will be such set of items that their total weight will equal ai. We assume that there is the infinite number of items of any weight. You can put multiple items of the same weight in one bag.
- For any set of items that have total weight less than or equal to m, there is a bag into which you can put this set. Similarly, a set of items can contain multiple items of the same weight.
- Of all sets of the items' weights that satisfy points 1 and 2, find the set with the minimum number of weights. In other words, value k should be as small as possible.
Find and print the required set.
The first line contains space-separated integers n and m (1 ≤ n, m ≤ 106). The second line contains ndistinct space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ m) — the bags' weight limits.
In the first line print "NO" (without the quotes) if there isn't set pi, that would meet the conditions.
Otherwise, in the first line print "YES" (without the quotes), in the second line print an integer k (showing how many numbers are in the suitable set with the minimum number of weights), in the third line print kspace-separated integers p1, p2, ..., pk (1 ≤ p1 < p2 < ... < pk). If there are multiple solutions, print any of them.
6 10
5 6 7 8 9 10
YES
5
5 6 7 8 9
1 10
1
NO
1 10
6
YES
1
6
——————————————————————————
然后来谈谈这道题了
题面是给n个篮子,要求制造不限制数目的商品,要求这些商品任意组合(可重复)的和在<m时总能找到一个容量等于和的篮子
有解输出YES,个数,商品的值,多组解要求输出篮子数最少,没有输出NO
首先按照题意来说我们随便组合这些商品,获得的值都有一个篮子来装
那么如果我们按每个篮子的值制造一种商品,每个篮子的值都可以由一个原商品,或两个原商品得到
为什么不是三个或以上,建设是三个组成这个篮子值A的话其中两个组成的商品会有一个篮子B来装,按照我们的条件,这个篮子值B的商品已经被制造出来了,所以假设不成立
也就是这样——
(栗子)
n=3 m=3
篮子:1 2 3
我们制造出的商品:1 2 3
我们发现1 1 1可以填满容量为3的篮子
但是1 1组合是2 ,我们已经制造出了2
可以是3=1+2,当然也有3=3
这个说明我们只要这样制造商品,每个商品和所有商品(包括自身)重新组合,就可以得到所有m以内商品和的种类 ——
(例子)
n=3 m=6(修改m)
篮子:1 2 3
我们制造出的商品:1 2 3
可搭配出
1+1=2 1+2=3 1+3=4
2+1=3 2+2=4 2+3=5
3+1=4 3+2=5 3+3=6
我们通过这些值可以判断,创造出的4 5 6无法装下,所以是NO
如果m=3
n=3 m=3
篮子:1 2 3
我们制造出的商品:1 2 3
可搭配出
1+1=2 1+2=3 1+3=4
2+1=3 2+2=4 2+3=5
3+1=4 3+2=5 3+3=6
我们发现2 3能被重新组合出来所以我们贪心地删除2 3 答案就是1
2用来构建3的,怎么被删除了呢
如果2被删除,说明2也可以被构建,故删除
所以根据xa*xb=xa+b
我们可以根据(xa1*xa2...xan)2来判断两两组合的所有解,满足卷积,所以FFT
【两个版本的FFT↓】
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
typedef long long ll;
const double PI=3.14159265358979323;//似乎精度要大大大大大大大
using namespace std;
struct complex {
double r,i;
complex(double real=0.0,double image=0.0) {
r=real;i=image;
}
complex operator + (const complex &a)const {
return complex(r+a.r,i+a.i);
}
complex operator - (const complex &a)const {
return complex(r-a.r,i-a.i);
}
complex operator * (const complex &a) const{
return complex(r*a.r-i*a.i,r*a.i+i*a.r);
}
}num1[],num2[];
void brc(complex *p,int l) {
int j=l/;
for(int i=;i<l-;++i) {//l-1是因为如果最后减完k=0,j=0
if(i<j) swap(p[i],p[j]);
int k=l/;
while(j>=k) {
j-=k;
k>>=;
}
if(j<k) j+=k;
}
}
//迭代版
void FFT(complex *p,int l,double flag) {
brc(p,l);
complex u,t;
for(int h=;h<=l;h<<=) {
complex wn(cos(flag**PI/h),sin(flag**PI/h));
for(int k=;k<l;k+=h){
complex w(,);
for(int j=k;j<k+h/;++j) {
u=p[j];
t=w*p[j+h/];
p[j]=u+t;
p[j+h/]=u-t;
w=w*wn;
}
}
}
}
//递归版
/*complex tmp[505];
void FFT(complex *p,int dep,int l,double flag) {
if((1<<dep)==l) return;
int step=1<<dep;
FFT(p,dep+1,l,flag);
FFT(p+step,dep+1,l,flag);
int newlen=l>>dep,half=newlen>>1;
complex wn(cos(2*flag*PI/newlen),sin(2*flag*PI/newlen));
complex w(1,0);
for(int i=0,s=0;i<half;++i,s+=(step<<1)) {
complex a=p[s];
complex b=w*p[s+step];
tmp[i]=a+b;
tmp[i+half]=a-b;
w=w*wn;
}
for(int i=0;i<newlen;++i) {
p[i<<dep]=tmp[i];
}
}//IDFT要除l*/
bool ok;
int a,b[],ans[],ac[],cnt;
int n,m;
void solve() {
scanf("%d%d",&n,&m);
int l=;
memset(b,,sizeof(b));
siji(i,,n) {
scanf("%d",&a);
b[a]=;
}
while(l<*m) {l=l<<;}
xiaosiji(i,,m) {
num1[i]=complex((double)b[i],0.0); }
FFT(num1,l,);
xiaosiji(i,,l) {
num2[i]=num1[i];
}
xiaosiji(i,,l) {
num1[i]=num1[i]*num2[i];
}
FFT(num1,l,-);
for(int z=;z<l;++z) num1[z].r/=l;
ok=true;
siji(i,,m) {
ans[i]=(int)(num1[i].r+0.5);
}
//为什么我这个sb写了个进位??????
//明明这不是大整数乘法啊QAQ
siji(i,,m) {
if(ans[i]>) {
if(b[i]==) {b[i]=;}
else if(b[i]==) {ok=;}
}
}
if(ok) {
puts("YES");
siji(i,,m) {
if(b[i]>) ac[++cnt]=i;
}
printf("%d\n",cnt);
siji(i,,cnt) {
printf("%d%c",ac[i]," \n"[i==cnt]);
}
}
else{
puts("NO");
}
}
int main(int argc, char const *argv[])
{
//freopen("f1.in","r",stdin);
solve();
return ;
}
codeforces 286 E. Ladies' Shop (FFT)的更多相关文章
- Codeforces 286E - Ladies' Shop(FFT)
Codeforces 题面传送门 & 洛谷题面传送门 好久没刷过 FFT/NTT 的题了,写篇题解罢( 首先考虑什么样的集合 \(T\) 符合条件.我们考察一个 \(x\in S\),根据题意 ...
- 快速傅里叶(FFT)的快速深度思考
关于按时间抽取快速傅里叶(FFT)的快速理论深度思考 对于FFT基本理论参考维基百科或百度百科. 首先谈谈FFT的快速何来?大家都知道FFT是对DFT的改进变换而来,那么它究竟怎样改进,它改进的思想在 ...
- 【BZOJ3527】力(FFT)
[BZOJ3527]力(FFT) 题面 Description 给出n个数qi,给出Fj的定义如下: \[Fj=\sum_{i<j}\frac{q_i q_j}{(i-j)^2 }-\sum_{ ...
- 【BZOJ4827】【HNOI2017】礼物(FFT)
[BZOJ4827][HNOI2017]礼物(FFT) 题面 Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一 个送给她.每 ...
- FFT/NTT总结+洛谷P3803 【模板】多项式乘法(FFT)(FFT/NTT)
前言 众所周知,这两个东西都是用来算多项式乘法的. 对于这种常人思维难以理解的东西,就少些理解,多背板子吧! 因此只总结一下思路和代码,什么概念和推式子就靠巨佬们吧 推荐自为风月马前卒巨佬的概念和定理 ...
- 【BZOJ4503】两个串(FFT)
[BZOJ4503]两个串(FFT) 题面 给定串\(S\),以及带通配符的串\(T\),询问\(T\)在\(S\)中出现了几次.并且输出对应的位置. \(|S|,|T|<=10^5\),字符集 ...
- 【BZOJ4259】残缺的字符串(FFT)
[BZOJ4259]残缺的字符串(FFT) 题面 给定两个字符串\(|S|,|T|\),两个字符串中都带有通配符. 回答\(T\)在\(S\)中出现的次数. \(|T|,|S|<=300000\ ...
- 【51Nod1258】序列求和V4(FFT)
[51Nod1258]序列求和V4(FFT) 题面 51Nod 多组数据,求: \[Ans=\sum_{i=1}^ni^k,n\le 10^{18},k\le50000\] 题解 预处理伯努利数,时间 ...
- 【CF528D】Fuzzy Search(FFT)
[CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...
随机推荐
- python---基础知识回顾(十一)图像处理模块PIL
前戏: 虽然PIL没有入OpenCV那样强大的功能,但是所提供的功能,在一般的图像处理中足够使用. 图像类别: 计算机绘图中有两类图像:一类是矢量图,另一类是点阵图(位图) 矢量图:基于计算机数字对象 ...
- git爬坑不完全指北(二):failed to push some refs to ‘XXX’的解决方案
报错分析 从字面理解,这个报错的意思就是说远程仓库里有一个改动是本地仓库里没有的,所以在push前要先把远程仓库上的改动pull或者fetch到本地仓库.然后再执行push的操作,把本地 ...
- bzoj千题计划140:bzoj4519: [Cqoi2016]不同的最小割
http://www.lydsy.com/JudgeOnline/problem.php?id=4519 最小割树 #include<queue> #include<cstdio&g ...
- Jmeter javaRequest插件开发
1. 适用场景 Jmeter工具当前支持的协议或协议所支持的传输方式及传输内容不能满足当前项目的测试要求时,就需要根据实际要求手动编写java测试代码(实现对应的Jmeter规范),以插件方式加载到J ...
- SELECT INTO 和 INSERT INTO SELECT比较
Insert是T-sql中常用语句,但我们在开发中经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用S ...
- angularJS $http $q $promise
一天早晨,爹对儿子说:“宝儿,出去看看天气如何!” 每个星期天的早晨,爹都叫小宝拿着超级望远镜去家附近最高的山头上看看天气走势如何,小宝说没问题,我们可以认为小宝在离开家的时候给了他爹一个promis ...
- 把JS和CSS合并到1个文件
合并JS文件和CSS文件很多人都知道,也用过,目的是为了减少请求数.但有时候我们觉的把JS合并到1个文件,CSS又合并到另外1个文件也是浪费,我们如何能把CSS和JS一起合并进1个文件了? 这里需要使 ...
- 《廖雪峰Git教程》学习笔记
原文链接 一.创建版本库 ①初始化一个Git仓库:git init ②添加文件到Git仓库:1.git add<file> ; 2.git commit 二.时光机穿梭 ①查看工作区状态 ...
- 已知可生成0~4的rand5(),实现生成0~6的rand7()
若已知生成0~6的rand7(),求生成0~4的rand5(),则一个方法就是不断生成0~7的数,直到这个数满足0~4就返回. int rand5(){ int res; do{ res = rand ...
- ubuntu下使用qemu模拟ARM(六)------驱动程序【转】
转自:http://blog.csdn.net/rfidunion/article/details/54709843 驱动程序分为在ubuntu上运行和在ARM开发板上运行两种,我们分别来进行测试 1 ...