cf286e Ladies ' Shop

Source: Internet
Author: User

Test instructions

For a waiting set {AI}, use it to complete the backpack for all the numbers in the 1-m , and can fill a pack of n sizes;

Now give n kinds of backpack {b1...bn} and M, find the minimum size of this set and give a scheme arbitrarily;


Exercises

The simplest set for this problem is {bi}, and if the {BI} collection is illegal, the original problem is not solved;

Judging this set is not legal equivalent to the existence of two elements in the set i,j and I+j? The set (I+J<=M);

Directly done is O (n^2), and can be found to convert the original B array into a generating function, the problem can be optimized with convolution;

A good judgment has a solution, then is to optimize the set, so that the elements inside the least;

The naïve idea is to enumerate the elements from small to large, and then determine whether the element can be represented by an already selected element, or the element will be selected, otherwise not added;

The time complexity of the algorithm is O (n*m), each time to do a complete backpack;

And we can find that it is optional to judge this element is not so troublesome, because given the B array has been the backpack completed, so only need to determine whether the current number can be two i,j, the same is the FFT solution;

Time complexity O (MLOGM);


Code:


#include <math.h> #include <stdio.h> #include <string.h> #include <algorithm> #define N 2097152using 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&LT;LEN;I+=H) {CP W (1,0), temp; for (J=0;J&LT;H&GT;&GT;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");p rintf ("%d\n", top), for (i=1;i<=top;i++) printf ("%d", St[i]); return 0;}



cf286e Ladies ' Shop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.