CF286E Ladies' Shop

题意:

对于一个待求的集合{ai},用其对1-m中的所有数做完全背包,能装满n种大小的背包;

现给出n种背包{b1...bn}和m,求这个集合最小的大小并任意给出一种方案;

题解:

对于这个题最简单的集合就是{bi},而如果{bi}集合不合法的话,原问题就无解了;

判断这个集合不合法等价于集合中存在两个元素i,j且i+j?该集合 (i+j<=m);

直接做是O(n^2)的,而可以发现将原b数组转成生成函数之后,这个问题就可以用卷积优化了;

判断好了有解,之后就是优化这个集合,让里面的元素最少了;

朴素的想法是从小到大枚举元素,然后判断这个元素能否被已经选中的元素表示,能则选中这个元素,否则不加入;

这个算法时间复杂度是O(n*m)的,每次都要做一遍完全背包;

而我们可以发现,判断这个元素是可选的不必这么麻烦,因为给出b数组的已经背包完成了,所以只需要判断当前数能否被两个i,j表示,一样是FFT解决;

时间复杂度O(mlogm);

代码:

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 2097152
using namespace std;
const double pi=acos(-1.0);
struct cp
{
	double x,y;
	cp(){}
	cp(double _,double __):x(_),y(__){}
	friend cp operator +(cp a,cp b)
	{
		return cp(a.x+b.x,a.y+b.y);
	}
	friend cp operator -(cp a,cp b)
	{
		return cp(a.x-b.x,a.y-b.y);
	}
	friend cp operator *(cp a,cp b)
	{
		return cp(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
}a[N],b[N];
void FFT(cp *a,int len,int type)
{
	int i,j,h,t;
	for(i=0,t=0;i<len;i++)
	{
		if(i>=t)	swap(a[i],a[t]);
		for(j=len>>1;(t^=j)<j;j>>=1);
	}
	for(h=2;h<=len;h<<=1)
	{
		cp wn(cos(2*pi*type/h),sin(2*pi*type/h));
		for(i=0;i<len;i+=h)
		{
			cp w(1,0),temp;
			for(j=0;j<h>>1;j++,w=w*wn)
			{
				temp=w*a[i+j+(h>>1)];
				a[i+j+(h>>1)]=a[i+j]-temp;
				a[i+j]=a[i+j]+temp;
			}
		}
	}
	if(type==-1)
		for(i=0;i<len;i++)
			a[i].x/=len;
}
int st[N],top;
bool A[N],B[N];
int main()
{
	int n,m,len,i,k;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&k);
		a[k].x=1;
		A[k]=1;
	}
	for(i=1<<25;i;i>>=1)
		if(m&i)
			{len=i<<2;break;}
	FFT(a,len,1);
	for(i=0;i<len;i++)
		b[i]=a[i]*a[i];
	FFT(b,len,-1);
	for(i=1;i<=m;i++)
	{
		if(!A[i]&&b[i].x>1e-5)
		{
			puts("NO");
			return 0;
		}
		if(A[i]&&b[i].x<1e-5)
		{
			st[++top]=i;
		}
	}
	puts("YES");
	printf("%d\n",top);
	for(i=1;i<=top;i++)
		printf("%d ",st[i]);
	return 0;
}

CF286E Ladies' Shop

时间: 2024-08-25 07:58:45

CF286E Ladies' Shop的相关文章

Ladies&#39; Shop

题意: 有 $n$ 个包,设计最少的物品体积(可重集),使得 1. 对于任意一个总体积不超过给定 $m$ 的物体集合有其体积和 恰好等于一个包的容量. 2.对于每一个包,存在一个物品集合能恰好装满它. 解法: 考虑对于包的容量集合建立多项式 $A(x)$ 注意到显然答案中的物品体积取自 $n$ 个包的容量. 那么根据题意有 条件2 <-> [$A(x)$中系数i为零 ->  $A^2(x)$ 中系数i为零] proof : 在只考虑系数是否为零的合法情况下,$A^k(x)$ 随着 $k$

hdu 4122 Alice&#39;s mooncake shop

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4122 题意大意是Alice开着一家月饼店,可以接到n做月饼的订单,而Alice只有在从2000年一月一日0点为第一个小时开始的前m个小时内做月饼,而且只能在整点 的时候做月饼,并且做月饼不花费时间,也就是一瞬间就可以做超级多的月饼,而每个月饼有t个小时的保质期,每个月饼保存每小时需要花费s元,而在可以 做月饼的m小时内,不同小时做月饼花费的钱也不同,每个订单都有交付订单嗯达时间,某年某月某日某时交付该

HDU 4122 Alice&#39;s mooncake shop 单调队列优化dp

Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4122 Description The Mid-Autumn Festival, also known as the Moon Festival or Zhongqiu Festival is a popular harvest festival celebrated by Ch

uva 1175 Ladies&#39; Choice (稳定婚姻问题)

uva 1175 Ladies' Choice Background Teenagers from the local high school have asked you to help them with the organization of next year?s Prom. The idea is to find a suitable date for everyone in the class in a fair and civilized way. So, they have or

WEDDING SHOP OPENCART 自适应主题模板 ABC-0663

WEDDING SHOP OPENCART 自适应主题模板 ABC-0663 Features :? Responsive theme,? Compatible with the most common browsers: FireFox 1.5-3.5.6+, IE9+, Safari 3.2 +, Opera, Google Chrome.? Compatible with Open Cart Version v2.x,? Made with HTML 5 and CSS 3? Design

HDU 4122 Alice&#39;s mooncake shop --RMQ

题意: 一个月饼店做月饼,总营业时间m小时,只能在整点做月饼,可以做无限个,不过在不同的时间做月饼的话每个月饼的花费是不一样的,假设即为cost[i],再给n个订单,即为在某个时间要多少个月饼,时间从2000年1月1日0时开始计算,必须在每个订单的时间之前完成这么多月饼,月饼还有保质期T小时以及保存费用S每小时,现在问满足这n个点的最小成本是多少. 解法: 因为月饼有保质期T,所以第i个月饼只能在[Ti-T+1,Ti]时间内做好.如果时间j有订单,假设在时间i做月饼是最好的,那么这个订单每个月饼

sgu 104 Little Shop of Flowers

经典dp问题,花店橱窗布置,不再多说,上代码 #include <cstdio> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #define N 150 #define inf 0x7f7f7f7f using namespace std; int n, m; int val[N][N], f[N][N]; int fa[N][N];

[POJ1157]LITTLE SHOP OF FLOWERS

试题描述 You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutiv

CodeForces 586B Laurenty and Shop

F - Laurenty and Shop Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 586B Description A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hu