BZOJ3771: Triple

BZOJ3771: Triple

https://lydsy.com/JudgeOnline/problem.php?id=3771

分析:

  • 由于最多拿走\(3\)个,可以手推容斥。
  • 然后就非常简单了,把桶卷在一起即可。
  • 具体的式子详见代码。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
#define N 215000
typedef long long ll;
typedef double f2;
const f2 pi=acos(-1);
char buf[1000000],*p1,*p2;
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
__attribute__((optimize("-O2")))inline int rd() {
    register int x=0;register char c=nc();
    while(c<'0'||c>'9') c=nc();
    while(c>='0'&&c<='9') x=((x+(x<<2))<<1)+c-'0',c=nc();
    return x;
}
struct cp {
	f2 x,y;
	cp() {}
	cp(f2 x_,f2 y_) {x=x_,y=y_;}
	cp operator + (const cp &u) const {return cp(x+u.x,y+u.y);}
	cp operator - (const cp &u) const {return cp(x-u.x,y-u.y);}
	cp operator * (const cp &u) const {return cp(x*u.x-y*u.y,x*u.y+y*u.x);}
	cp operator / (const cp &u) const {return cp(x/u.x,y/u.x);}
	cp operator / (f2 u) const {return cp(x/u,y/u);}
}A[N],B[N];
char pbuf[1000000],*pp=pbuf;
void push(const char c) {
    if(pp-pbuf==1000000) fwrite(pbuf,1,1000000,stdout),pp=pbuf;
    *pp++=c;
}
void write(int x) {
    static int sta[35];
    int top=0;
    do{sta[top++]=x%10,x/=10;}while(x);
    while(top) push(sta[--top]+'0');push(' ');
}
void write1(ll x) {
    static int sta1[60];
    int top=0;
    do{sta1[top++]=x%10,x/=10;}while(x);
    while(top) push(sta1[--top]+'0');push('\n');
}
void fft(cp *a,int len,int flg) {
	int i,j,k,t;
	cp w,wn,tmp;
	for(i=k=0;i<len;i++) {
		if(i>k) swap(a[i],a[k]);
		for(j=len>>1;(k^=j)<j;j>>=1) ;
	}
	for(k=2;k<=len;k<<=1) {
		wn=cp(cos(2*pi*flg/k),sin(2*pi*flg/k));
		t=k>>1;
		for(i=0;i<len;i+=k) {
			w=cp(1,0);
			for(j=i;j<i+t;j++) {
				tmp=a[j+t]*w;
				a[j+t]=a[j]-tmp;
				a[j]=a[j]+tmp;
				w=w*wn;
			}
		}
	}
	if(flg==-1) for(i=0;i<len;i++) a[i].x/=len;
}
int h[N][3],n,mx;
ll ans[N];
int main() {
	int x;
	n=rd();
	int i,mx=0;
	for(i=1;i<=n;i++) {
		x=rd();
		h[x][0]++;
		h[x*2][1]++;
		h[x*3][2]++;
		mx=max(mx,x);
	}
	int lim=mx*3;
	int len=1;
	while(len<=lim) len<<=1;
	for(i=0;i<=lim;i++) A[i].x=h[i][0],B[i].x=h[i][1];
	fft(A,len,1); fft(B,len,1);
	cp t3=cp(3,0);
	for(i=0;i<len;i++) A[i]=(A[i]*A[i]*A[i]-t3*A[i]*B[i])/6+(A[i]*A[i]-B[i])/2+A[i];
	fft(A,len,-1);
	for(i=0;i<=lim;i++) {
		ans[i]=A[i].x+0.1+h[i][2]/3.0+0.1;
		if(ans[i]) {
			write(i); write1(ans[i]);
		}
	}
	fwrite(pbuf,1,pp-pbuf,stdout);
}

posted @ 2019-01-01 14:02  fcwww  阅读(164)  评论(0编辑  收藏  举报