传送门
题目大意:给出n个互不相同的数,问从中选出1/2/3个数,每一个可以组合出的和有多少种方案。
首先1个的直接统计
将所有的数搞成一个生成函数,做一遍卷积搞出来选2个的答案
但是2个的存在选了两个相同的,或者选了一个排列,直接除2即可
然后生成函数卷两次统计选3个的答案
这里需要容斥一下,(选3个的答案-强行选了2个一样的*3+强行选了3个一样的*2)/6才是不考虑顺序、选不重复的3个的答案
强行选了3个一样的直接枚举,强行选了2个一样的就将每一个数的两倍搞成生成函数再和1的卷一下求出
用FFT加速
#include
#include
#include
#include
#include
using namespace std;
#define N 300005
const double pi=acos(-1.0);
int cnt,Max;
int val[N],n,m,L,R[N],ans3[N],F[N];
struct complex
{
double x,y;
complex(double X=0,double Y=0)
{
x=X,y=Y;
}
}a[N],b[N];
complex operator + (complex a,complex b) {return complex(a.x+b.x,a.y+b.y);}
complex operator - (complex a,complex b) {return complex(a.x-b.x,a.y-b.y);}
complex operator * (complex a,complex b) {return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
void FFT(complex a[N],int opt)
{
for (int i=0;iif (ifor (int k=1;k1)
{
complex wn=complex(cos(pi/k),opt*sin(pi/k));
for (int i=0;i1))
{
complex w=complex(1,0);
for (int j=0;jcomplex x=a[i+j],y=w*a[i+j+k];
a[i+j]=x+y,a[i+j+k]=x-y;
}
}
}
}
int main()
{
scanf("%d",&cnt);
for (int i=1;i<=cnt;++i)
scanf("%d",&val[i]),Max=max(Max,val[i]),++F[val[i]];
// 1把
m=Max<<1;L=0;
for (n=1;n<=m;n<<=1) ++L;
for (int i=0;i>1]>>1)|((i&1)<<(L-1));
for (int i=0;i<=n;++i) a[i]=b[i]=complex(0,0);
for (int i=1;i<=cnt;++i) a[val[i]].x=b[val[i]].x=1.0;
FFT(a,1);FFT(b,1);
for (int i=0;i<=n;++i) a[i]=a[i]*b[i];
FFT(a,-1);
for (int i=0;i<=n;++i) a[i].x/=n;
for (int i=0;i<=n;++i) F[i]+=(int)(a[i].x+0.5)>>1;
// 2把
m=Max*3;L=0;
for (n=1;n<=m;n<<=1) ++L;
for (int i=0;i>1]>>1)|((i&1)<<(L-1));
for (int i=0;i<=n;++i) b[i]=complex(0,0);
for (int i=1;i<=cnt;++i) b[val[i]].x=1.0;
FFT(a,1);FFT(b,1);
for (int i=0;i<=n;++i) a[i]=a[i]*b[i];
FFT(a,-1);
for (int i=0;i<=n;++i) a[i].x/=n;
for (int i=0;i<=n;++i)
ans3[i]+=(int)(a[i].x+0.5);
// 所有的答案
m=Max<<2;L=0;
for (n=1;n<=m;n<<=1) ++L;
for (int i=0;i>1]>>1)|((i&1)<<(L-1));
for (int i=0;i<=n;++i) a[i]=b[i]=complex(0,0);
for (int i=1;i<=cnt;++i) a[val[i]<<1].x=1.0,b[val[i]]=1.0;
FFT(a,1);FFT(b,1);
for (int i=0;i<=n;++i) a[i]=a[i]*b[i];
FFT(a,-1);
for (int i=0;i<=n;++i) a[i].x/=n;
for (int i=0;i<=n;++i) ans3[i]-=(int)(a[i].x+0.5)*3;
// 强制2把一样,并且消除排列的影响
for (int i=1;i<=cnt;++i) ans3[val[i]*3]+=2;
// 强制3把都一样,并且消除排列的影响
for (int i=0;i<=n;++i) F[i]+=ans3[i]/6;
// 3把
for (int i=0;i<=Max*3;++i)
if (F[i]) printf("%d %d\n",i,F[i]);
}